// 피보나치 수 4 // https://w...content-available-to-author-only...c.net/problem/10826 #include<iostream> #include<vector> #include<string> using namespace std; class num { private: vector<long long> n; const static int MAX = (int)1e9; const static int MAXLEN = 9; public: num() {} num(long long _n) { if(!_n) n.push_back(0); while(_n) { n.push_back(_n%MAX); _n/=MAX; } } ~num() { n.clear(); } const num operator+ (const num& op) const { num res; int size = n.size(), sizeOP = op.n.size(); int N = size > sizeOP ? size : sizeOP; int carry = 0; for(int i=0; i<N; i++) { int a = (i<size?n[i]:0) + (i<sizeOP?op.n[i]:0) + carry; if(a >= MAX) { carry = 1; a -= MAX; } else carry = 0; res.n.push_back(a); } if(carry) res.n.push_back(carry); return res; } const num operator* (const num& op) const { num res; int size = n.size(), sizeOP = op.n.size(); const num& b = (size < sizeOP ? op : *this); const num& s = (size < sizeOP ? *this : op); int bSize = size < sizeOP ? sizeOP : size; int sSize = size < sizeOP ? size : sizeOP; int resSize = bSize + sSize; if(b.n[bSize-1]*s.n[sSize-1] < MAX) resSize--; res.n.assign(resSize, 0); int carry = 0; for(int i=0; i<bSize; i++) for(int j=0; j<sSize; j++) { long long a = b.n[i] * s.n[j]; if(a >= MAX) { res.n[i+j+1] += a/MAX; a %= MAX; } res.n[i+j] += a; if(res.n[i+j]>=MAX) { res.n[i+j+1] += res.n[i+j]/MAX; res.n[i+j] %= MAX; } } return res; } void prt() const { int size = n.size(); cout << n[size-1]; for(int i=size-2; i>=0; i--) { string s = to_string(n[i]); int z = MAXLEN - s.size(); while(z--) cout << '0'; cout << s; } } }; typedef vector<vector<num>> Matrix; Matrix operator * (const Matrix& a, const Matrix& b) { int size = a.size(); Matrix res(size, vector<num>(size,0)); for(int i=0; i<size; i++) for(int j=0; j<size; j++) for(int k=0; k<size; k++) res[i][j] = res[i][j] + a[i][k] * b[k][j]; return res; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; if(n<=1) { cout << n; return 0; } Matrix ans = {{1,0},{0,1}}; Matrix I = {{1,1},{1,0}}; while(n) { if(n&1) ans = ans * I; I = I * I; n/=2; } ans[0][1].prt(); // num a(1), ans(0), t; // while(n--) { // t = ans + a; // ans = a; // a = t; // } // ans.prt(); return 0; }
5000
Standard output is empty
*** Error in `./prog': munmap_chunk(): invalid pointer: 0x00005646908a3270 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x2b364acacbcb] /lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x2b364acb2f96] ./prog(+0x120a)[0x56468fd1d20a] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x2b364ac5c2b1] ./prog(+0x14aa)[0x56468fd1d4aa] ======= Memory map: ======== 2b3649f5c000-2b3649f7f000 r-xp 00000000 fd:00 2710543 /lib/x86_64-linux-gnu/ld-2.24.so 2b3649f7f000-2b3649f83000 rw-p 00000000 00:00 0 2b3649f8c000-2b3649f91000 rw-p 00000000 00:00 0 2b364a17f000-2b364a180000 r--p 00023000 fd:00 2710543 /lib/x86_64-linux-gnu/ld-2.24.so 2b364a180000-2b364a181000 rw-p 00024000 fd:00 2710543 /lib/x86_64-linux-gnu/ld-2.24.so 2b364a181000-2b364a182000 rw-p 00000000 00:00 0 2b364a182000-2b364a2f4000 r-xp 00000000 fd:00 2712611 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2b364a2f4000-2b364a4f4000 ---p 00172000 fd:00 2712611 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2b364a4f4000-2b364a4fe000 r--p 00172000 fd:00 2712611 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2b364a4fe000-2b364a500000 rw-p 0017c000 fd:00 2712611 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22 2b364a500000-2b364a504000 rw-p 00000000 00:00 0 2b364a504000-2b364a607000 r-xp 00000000 fd:00 2710572 /lib/x86_64-linux-gnu/libm-2.24.so 2b364a607000-2b364a806000 ---p 00103000 fd:00 2710572 /lib/x86_64-linux-gnu/libm-2.24.so 2b364a806000-2b364a807000 r--p 00102000 fd:00 2710572 /lib/x86_64-linux-gnu/libm-2.24.so 2b364a807000-2b364a808000 rw-p 00103000 fd:00 2710572 /lib/x86_64-linux-gnu/libm-2.24.so 2b364a808000-2b364a81e000 r-xp 00000000 fd:00 2710510 /lib/x86_64-linux-gnu/libgcc_s.so.1 2b364a81e000-2b364aa1d000 ---p 00016000 fd:00 2710510 /lib/x86_64-linux-gnu/libgcc_s.so.1 2b364aa1d000-2b364aa1e000 r--p 00015000 fd:00 2710510 /lib/x86_64-linux-gnu/libgcc_s.so.1 2b364aa1e000-2b364aa1f000 rw-p 00016000 fd:00 2710510 /lib/x86_64-linux-gnu/libgcc_s.so.1 2b364aa1f000-2b364aa37000 r-xp 00000000 fd:00 2710529 /lib/x86_64-linux-gnu/libpthread-2.24.so 2b364aa37000-2b364ac36000 ---p 00018000 fd:00 2710529 /lib/x86_64-linux-gnu/libpthread-2.24.so 2b364ac36000-2b364ac37000 r--p 00017000 fd:00 2710529 /lib/x86_64-linux-gnu/libpthread-2.24.so 2b364ac37000-2b364ac38000 rw-p 00018000 fd:00 2710529 /lib/x86_64-linux-gnu/libpthread-2.24.so 2b364ac38000-2b364ac3c000 rw-p 00000000 00:00 0 2b364ac3c000-2b364add1000 r-xp 00000000 fd:00 2710666 /lib/x86_64-linux-gnu/libc-2.24.so 2b364add1000-2b364afd0000 ---p 00195000 fd:00 2710666 /lib/x86_64-linux-gnu/libc-2.24.so 2b364afd0000-2b364afd4000 r--p 00194000 fd:00 2710666 /lib/x86_64-linux-gnu/libc-2.24.so 2b364afd4000-2b364afd6000 rw-p 00198000 fd:00 2710666 /lib/x86_64-linux-gnu/libc-2.24.so 2b364afd6000-2b364afda000 rw-p 00000000 00:00 0 56468fd1c000-56468fd20000 r-xp 00000000 fd:00 14969794 /home/LLOX5u/prog 56468ff1f000-56468ff20000 r--p 00003000 fd:00 14969794 /home/LLOX5u/prog 56468ff20000-56468ff21000 rw-p 00004000 fd:00 14969794 /home/LLOX5u/prog 564690873000-5646908a5000 rw-p 00000000 00:00 0 [heap] 7ffeed743000-7ffeed764000 rw-p 00000000 00:00 0 [stack] 7ffeed7a0000-7ffeed7a2000 r-xp 00000000 00:00 0 [vdso] 7ffeed7a2000-7ffeed7a4000 r--p 00000000 00:00 0 [vvar] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]