Advertisement
Saleh127

Light oj(LO) 1213

Mar 19th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5.  
  6. ll bigmod(ll a,ll c,ll d)
  7. {
  8. if(c==0) return 1;
  9. ll x=bigmod(a,c/2,d);
  10. x=x*x%d;
  11. if(c%2==1)
  12. {
  13. x=(x*a)%d;
  14. }
  15. return x;
  16. }
  17.  
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(0);
  21. cin.tie(0);cout.tie(0);
  22.  
  23. test
  24. {
  25.  
  26.  
  27. ll n,k,i,j,l,mod,a[2000],sum=0;
  28.  
  29. cin>>n>>k>>mod;
  30.  
  31. for(i=0;i<n;i++)
  32. {
  33. cin>>a[i];
  34. sum+=a[i];
  35. }
  36.  
  37. l=(k*bigmod(n,k-1,mod))%mod;
  38. l=(l*(sum%mod))%mod;
  39.  
  40. cout<<"Case "<<cs<<": "<<l<<endl;
  41. }
  42.  
  43.  
  44. return 0;
  45. }
  46.  
  47. //the whole process will be added n ^ k times,
  48. //but each number is not added n ^ k times.
  49. //it's added n ^ (k-1) * k times,
  50. //So ans = sum * n ^ (k-1) * k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement