一看數據就是快速冪,一開始用int錯了,索性全改成long longphp
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 long long f(long long a) 6 { 7 long long temp=2; 8 long long ans=1; 9 while (a) 10 { 11 if (a%2!=0) 12 ans*=temp; 13 14 ans=ans%1000000007; 15 temp=temp*temp; 16 temp=temp%1000000007; 17 a/=2; 18 } 19 return ans; 20 } 21 int main() 22 { 23 long long n,m; 24 cin>>n; 25 while (n--) 26 { 27 cin>>m; 28 cout <<f(m-1)-1<<endl; 29 } 30 return 0; 31 }