bzoj1406: [AHOI2007]密码箱
1406: [AHOI2007]密码箱
Time Limit: 5 Sec Memory Limit: 64 MB
Description
Input
Output
Sample Input
Sample Output
5
7
11
HINT
Source
Tip:
原式化为(x+1)(x-1)=kn;
将n分解因数,取>√n的因数枚举就可以了;
Code:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<map> using namespace std; int n,m,ans[1000008],a[100008],tot; map<int,bool>mp; int main(){ scanf("%d",&n); ans[++ans[0]]=1; for(int i=1;i*i<=n;i++) if(n%i==0) a[++a[0]]=n/i; for(int i=1;i<=a[0];i++) for(int j=a[i];j<=n;j+=a[i]){ if((j+2)<n && (j+2)%(n/a[i])==0 && !mp[j+1]){ mp[j+1]=1; ans[++ans[0]]=j+1; } if((j-2)>0 && (j-2)%(n/a[i])==0 && !mp[j-1]){ mp[j-1]=1; ans[++ans[0]]=j-1; } } sort(ans+1,ans+ans[0]+1); for(int i=1;i<=ans[0];i++) printf("%d\n",ans[i]); }