Advertisement
Saleh127

UVA 11582

Mar 12th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll unsigned long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. ll f[1000005];
  6. ll mod(ll a,ll c,ll d)
  7. {
  8. if(c==0) return 1;
  9. ll x=mod(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. ///Pisano period: the sequence f(i) % n is periodic;
  19. ///we compute the period of the sequence f(i) % n;
  20. ///forall n; f(a^b) % p[n] = f((a^b) % p[n]) \% p[n];
  21. ///use modPow
  22.  
  23. int main()
  24. {
  25. ios_base::sync_with_stdio(0);
  26. cin.tie(0);
  27. cout.tie(0);
  28.  
  29. test
  30. {
  31. ll n,a,b,c=1,i,j,k,l;
  32.  
  33.  
  34.  
  35. cin>>a>>b>>n;
  36.  
  37. f[0]=0;
  38. f[1]=1%n;
  39.  
  40. for(i=2; ; i++)
  41. {
  42. f[i]=(f[i-2]+f[i-1])%n;
  43.  
  44. if(f[i]==f[1] && f[i-1]==f[0])
  45. {
  46. c=i-1;
  47. break;
  48. }
  49. }
  50.  
  51. k=mod(a%c,b,c);
  52.  
  53. cout<<f[k]<<endl;
  54.  
  55.  
  56. }
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement