Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define llong long long
  4.  
  5. using namespace std;
  6.  
  7. typedef vector<int> big;
  8.  
  9. const int MAXN = (int) 1e6 + 7;
  10. const int INF = (int) 1e9 + 23;
  11.  
  12. int plen = 1490280;
  13.  
  14. int bin(int x, long long y) {
  15. int res = 1;
  16. while (y > 0) {
  17. if (y & 1LL) res = (1ll * res * x) % INF;
  18. y >>= 1LL;
  19. x = (1ll * x * x) % INF;
  20. }
  21. return res;
  22. }
  23.  
  24. int phi(int x) {
  25. int res = x;
  26. for (int i = 2; i * i <= x; i++) {
  27. if (x % i == 0) {
  28. while (x % i == 0) {
  29. x /= i;
  30. }
  31. res -= res / i;
  32. }
  33. }
  34. if (x > 1)
  35. res -= res / x;
  36.  
  37. return res;
  38. }
  39.  
  40. int main() {
  41. #ifdef LOCAL
  42. freopen("in", "r", stdin);
  43. freopen("out","w",stdout);
  44. #endif
  45.  
  46. int t, n;
  47. int cur, prv, blocks, ans;
  48.  
  49. long long k;
  50.  
  51. scanf("%d", &t);
  52. while (t--) {
  53. scanf("%d%lld", &n, &k);
  54.  
  55. k %= phi(INF);
  56.  
  57. ans = 0;
  58. blocks = n / plen;
  59.  
  60. cur = prv = 1;
  61. for (int i = 3; i <= plen; i++) {
  62. cur += prv;
  63. if (cur >= INF)
  64. cur -= INF;
  65.  
  66. prv = cur - prv;
  67. if (prv < 0)
  68. prv += INF;
  69.  
  70. ans += bin(cur, k);
  71. if (ans >= INF)
  72. ans -= INF;
  73. }
  74. ans += 2;
  75. if (ans >= INF)
  76. ans -= INF;
  77.  
  78. ans = (1LL * ans * blocks) % INF;
  79.  
  80. n -= blocks * plen;
  81.  
  82. cur = prv = 1;
  83. for (int i = 3; i <= n; i++) {
  84. cur += prv;
  85. if (cur >= INF)
  86. cur -= INF;
  87.  
  88. prv = cur - prv;
  89. if (prv < 0)
  90. prv += INF;
  91.  
  92. ans += bin(cur, k);
  93. if (ans >= INF)
  94. ans -= INF;
  95. }
  96. if (n >= 1) ans++;
  97. if (n >= 2) ans++;
  98.  
  99. if (ans >= INF)
  100. ans -= INF;
  101.  
  102. printf("%d\n", ans);
  103. }
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement