首页 > 代码库 > Acdream a + b

Acdream a + b

http://acdream.info/problem?pid=1007

两个 long long 相乘会超long long

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 #define maxn 10000 6 #define mod 10000000007 7 using namespace std; 8  9 ll a[maxn];10 ll n,k,t;11 ll fMul(ll a, ll b) {12     ll t = 0, y = a;13     while(b) {14         if(b & 1) t = (t + y) % mod;15         y = (y + y) % mod;16         b >>= 1;17     }18     return t;19 }20 21 ll modExp(ll a, ll b) {22     ll t = 1, y = a;23     while(b) {24         if(b & 1) t = (fMul(t, y)) % mod;25         y = (fMul(y, y)) % mod;26         b >>= 1;27     }28     return t;29 }30 31 int main()32 {33       scanf("%lld",&t);34       while(t--)35       {36           scanf("%lld%lld",&n,&k);37           ll sum=0;38           for(int i=0; i<n; i++)39           {40               scanf("%lld",&a[i]);41           }42           for(int i=0; i<n; i++)43           {44               a[i]=((a[i]%mod)+mod)%mod;45               ll c=modExp(a[i],k);46               sum=(sum+c)%mod;47           }48           printf("%lld\n",sum);49       }50       return 0;51 }
View Code

 

Acdream a + b