HDU 2197 本原串
快速幂+找规律
#include<cstdio> #include<cstring> #include<cmath> #include<map> #include<algorithm> using namespace std; map<int,int>C; const int MOD=2008; int n; int fast_power(int a,int b) { int c=1; while(b!=0) { if(b%2==1) c=(c*a)%MOD,b--; else a=(a*a)%MOD,b=b/2; } return c; } int cal (int x) { int ans,i; if(C[x]!=0) return C[x]; else { ans=fast_power(2,x); for(i=1;i*i<=x;i++) { if(i==1) {ans=(ans+MOD-cal(1))%MOD;continue;} if(i*i==x&&i!=1) ans=(ans+MOD-cal(i))%MOD; if(i!=1&&i*i<x&&x%i==0) { ans=(ans+MOD-cal(i))%MOD; ans=(ans+MOD-cal(x/i))%MOD; } } } return ans; } int main() { C.clear(); C[1]=2; while(~scanf("%d",&n)) { if(C[n]==0) C[n]=cal(n)%MOD; printf("%d\n",C[n]); } return 0; }