快速幂 HDU3003
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 long long multi(long long a,long long b) 8 { 9 long long mod=b+1; 10 long long ret=1; 11 if(b==0) 12 return 1; 13 while(b>0) 14 { 15 if(b&1) 16 ret=ret*a%mod; 17 a=(a*a)%mod; 18 b=b/2; 19 } 20 return ret; 21 } 22 23 int main() 24 { 25 long long n; 26 while(cin>>n&&n!=0) 27 { 28 long long ans=(multi(2,n-1)+1)%n; 29 printf("%I64d\n",ans); 30 } 31 return 0; 32 }
注意大小,用long long 替换 int