An easy problem
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7963 Accepted Submission(s): 1920
Problem Description
When Teddy was a child , he was always thinking about some simple math problems ,such as “What it’s 1 cup of water plus 1 pile of dough ..” , “100 yuan buy 100 pig” .etc..
One day Teddy met a old man in his dream , in that dream the man whose name was“RuLai” gave Teddy a problem :
Given an N , can you calculate how many ways to write N as i * j + i + j (0 < i <= j) ?
Teddy found the answer when N was less than 10…but if N get bigger , he found it was too difficult for him to solve.
Well , you clever ACMers ,could you help little Teddy to solve this problem and let him have a good dream ?
Input
The first line contain a T(T <= 2000) . followed by T lines ,each line contain an integer N (0<=N <= 1010).
Output
For each case, output the number of ways in one line.
Sample Input
2 1 3
Sample Output
0 1
求满足n=i*j+i+j(0<i<=j)的i、j的种数。
第一种方法:首先这个等式可以化成(n+1)=(i+1)*(j+1),所以只要求出(n+1)的约数的种数即可。同时注意到i与j呈负相关,同时i小于等于j,所以只需要从2到sqrt(n+1)枚举(从2开始是因为i最小为1,我们枚举的是(i+1))。但是,好暴力啊。所以我们可以用筛法先保存1e5以内的素数,再通过质因子分解求出约数数量。
第二种方法:观察等式n=i*j+i+j,可以转化成n-i=(i+1)*j,发现暴力枚举i,判断(n-i)%(n+1)==0判断整数j是否存在也是可行的,同样是因为之前说过的的原因,只需要从1到sqrt(n)枚举,同时判断i<=(n-i)/(i+1),不符合即则跳出循环,因为题目规定i<=j。
/* (n+1)=(i+1)*(j+1) Memory: 1568 KB Time: 2184 MS Language: G++ Result: Accepted */ #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; typedef long long LL; const double eps=1e-6; const int MAXN=210; LL n; int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%lld",&n); n++; int ans=0; for(int i=2; (LL)i*i<=n; i++) { if(n%i==0) ans++; } printf("%d\n",ans); } return 0; }
/* n-i=(i+1)*j Memory: 1572 KB Time: 2059 MS Language: G++ Result: Accepted */ #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; typedef long long LL; const double eps=1e-6; const int MAXN=210; LL n; int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%lld",&n); if(n==1||n==2) { printf("0\n"); continue; } if(n==3) { printf("1\n"); continue; } int ans=0; double s=sqrt(n); for(LL i=1; i<=s; i++) { if(i>(n-i)/(i+1)) break; if((n-i)%(i+1)==0) ans++; } printf("%d\n",ans); } return 0; }
/* 质因子分解求出约数数量 Memory: 2004 KB Time: 78 MS Language: G++ Result: Accepted */ #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; typedef long long LL; const double eps=1e-6; const int MAXN=1e5+20; int prime[MAXN],vis[MAXN],cnt; LL n; int getPrime() { cnt=1; vis[1]=1; for(int i=2;i<=MAXN;i++) { if(vis[i]) continue; prime[cnt++]=i; if((LL)i*i>MAXN) continue; for(int j=i*i;j<MAXN;j+=i) vis[j]=1; } } int main() { getPrime(); int tcase; scanf("%d\n",&tcase); while(tcase--) { scanf("%lld",&n); int ans=1; n++; for(int i=1;(LL)prime[i]*prime[i]<=n;i++) { if(n%prime[i]==0) { int s=0; while(n%prime[i]==0) { s++; n/=prime[i]; } ans*=(s+1); } if(n==1) break; } if(n!=1) ans*=2; printf("%d\n",(ans+1)/2-1); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。