fork download
  1. #include "bits/stdc++.h"
  2. #define up(i,a,b) for (int i = (int)a; i <= (int)b; i++)
  3. using namespace std;
  4.  
  5. string addBig(string a, string b){
  6. string res = "";
  7. while (a.size() < b.size()) a = "0" + a;
  8. while (b.size() < a.size()) b = "0" + b;
  9. int cr = 0;
  10. for (int i = a.size()-1; i >= 0; i--){
  11. int t = a[i] + b[i] - 48*2 + cr;
  12. cr = t/10;
  13. t = t % 10;
  14. res = (char)(t+48) + res;
  15. }
  16. if (cr) res = '1' + res;
  17. return res;
  18. }
  19.  
  20. string mulBig(string a, string b){
  21. string res = "";
  22. int n = a.size();
  23. int m = b.size();
  24. int len = n + m - 1;
  25. int cr = 0;
  26. for (int i = len; i >= 0; i--){
  27. int t = 0;
  28. for (int j = n-1; j >= 0; j--){
  29. if (i-j <= m && i-j >= 1){
  30. int a1 = a[j] - 48;
  31. int b1 = b[i-j-1] - 48;
  32. t += a1*b1;
  33. }
  34. }
  35. t += cr;
  36. cr = t/10;
  37. res = (char)(t % 10 + 48) + res;
  38. }
  39. while (res.size() > 1 && res[0] == '0') res.erase(0,1);
  40. return res;
  41. }
  42.  
  43. long long n,M;
  44. struct Matrix{
  45. string c[2][2];
  46. Matrix(){
  47. c[0][0] = "1"; c[0][1] = "1";
  48.  
  49. c[1][0] = "1"; c[1][1] = "0";
  50. }
  51. ~Matrix(){};
  52. }; Matrix A;
  53.  
  54. Matrix nhan(Matrix a, Matrix b){
  55. Matrix res;
  56. for (int i = 0; i <= 1; i++){
  57. for (int j = 0; j <= 1; j++){
  58. res.c[i][j] = "0";
  59. for (int k = 0; k <= 1; k++){
  60. res.c[i][j] = addBig(res.c[i][j], mulBig(a.c[i][k], b.c[k][j]));
  61. }
  62. }
  63. }
  64. return res;
  65. }
  66.  
  67. Matrix Fpow(Matrix& A, int n){
  68. Matrix res;
  69. up(i,0,1) up(j,0,1) res.c[i][j] = "0";
  70. up(i,0,1) res.c[i][i] = "1";
  71. for (; n; n >>= 1, A = nhan(A, A)){
  72. if (n & 1) res = nhan(res, A);
  73. }
  74. return res;
  75. }
  76.  
  77. string a,b;
  78. #define Task "A"
  79. signed main(){
  80. ios_base::sync_with_stdio(false);
  81. cin.tie(0);
  82. if (fopen(Task".inp", "r")){
  83. freopen(Task".inp", "r", stdin);
  84. freopen(Task".out", "w", stdout);
  85. }
  86.  
  87. int tt;
  88. cin >> tt;
  89. while (tt--){
  90. int n;
  91. cin >> n;
  92. Matrix A;
  93. A = Fpow(A, n);
  94. cout << A.c[0][0] << "\n";
  95. }
  96. }
Success #stdin #stdout 0.1s 5528KB
stdin
1
10000
stdout
54438373113565281338734260993750380135389184554695967026247715841208582865622349017083051547938960541173822675978026317384359584751116241439174702642959169925586334117906063048089793531476108466259072759367899150677960088306597966641965824937721800381441158841042480997984696487375337180028163763317781927941101369262750979509800713596718023814710669912644214775254478587674568963808002962265133111359929762726679441400101575800043510777465935805362502461707918059226414679005690752321895868142367849593880756423483754386342639635970733756260098962462668746112041739819404875062443709868654315626847186195620146126642232711815040367018825205314845875817193533529827837800351902529239517836689467661917953884712441028463935449484614450778762529520961887597272889220768537396475869543159172434537193611263743926337313005896167248051737986306368115003088396749587102619524631352447499505204198305187168321623283859794627245919771454628218399695789223798912199431775469705216131081096559950638297261253848242007897109054754028438149611930465061866170122983288964352733750792786069444761853525144421077928045979904561298129423809156055033032338919609162236698759922782923191896688017718575555520994653320128446502371153715141749290913104897203455577507196645425232862022019506091483585223882711016708433051169942115775151255510251655931888164048344129557038825477521111577395780115868397072602565614824956460538700280331311861485399805397031555727529693399586079850381581446276433858828529535803424850845426446471681531001533180479567436396815653326152509571127480411928196022148849148284389124178520174507305538928717857923509417743383331506898239354421988805429332440371194867215543576548565499134519271098919802665184564927827827212957649240235507595558205647569365394873317659000206373126570643509709482649710038733517477713403319028105575667931789470024118803094604034362953471997461392274791549730356412633074230824051999996101549784667340458326852960388301120765629245998136251652347093963049734046445106365304163630823669242257761468288461791843224793434406079917883360676846711185597501