a53

giovanacci

a53
May 4th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <fstream>
  2. const int MAX_MOD=100000;
  3. using namespace std;
  4. ifstream cin("giovanacci.in");
  5. ofstream cout("giovanacci.out");
  6. int fib[4*MAX_MOD+1];
  7.  
  8. long long gcd(long long a,long long b)
  9. {
  10. return b?gcd(b,a %b):a;
  11. }
  12.  
  13. int getPeriod(int mod)
  14. {
  15. fib[0]=0;
  16. fib[1]=1;
  17. int i=2;
  18. do
  19. {
  20. fib[i]=(fib[i-1]+fib[i-2])%mod;
  21. i++;
  22. } while (fib[i-1]!=1||fib[i-2]!=0);
  23. return i-2;
  24. }
  25.  
  26. int main()
  27. {
  28. int T,mod;
  29. int n;
  30. long long x,q;
  31. int period;
  32. cin>>T>>mod;
  33. period=getPeriod(mod);
  34. while(T--)
  35. {
  36. cin>>n>>q;
  37. for(int i=1;i<n;i++)
  38. {
  39. cin>>x;
  40. q=gcd(q,x);
  41. }
  42. cout<<fib[q%period]<<'\n';
  43. }
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment