結果

問題 No.2156 ぞい文字列
ユーザー lddlinanlddlinan
提出日時 2023-03-18 16:13:45
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,467 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 86,196 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 17:23:09
合計ジャッジ時間 1,825 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

bool rcmp(int a, int b) { return a>b; }
typedef long long LL;
class mypcmp {
public:
    bool operator()(const int& a, const int& b) {
        return a<b;
    }
};

#define MOD 998244353
int bb[2][2], mx[2][2], tmx[2][2];

int fib(LL n) {
    if (n<=1) return 1;
    int i, j, k;
    bb[0][0]=1; bb[0][1]=1; bb[1][0]=1; bb[1][1]=0;
    LL e=n-1;
    mx[0][0]=1; mx[0][1]=0; mx[1][0]=0; mx[1][1]=1;
    LL r, t, v;
    while(e) {
        if (e&1) {
            for (i=0; i<2; i++) for (j=0; j<2; j++) {
                v=0; for (k=0; k<2; k++) { t=mx[i][k]; t*=bb[k][j]; t%=MOD; v+=t; v%=MOD; }
                tmx[i][j]=v;
            }
            for (i=0; i<2; i++) for (j=0; j<2; j++) mx[i][j]=tmx[i][j];
        }
        for (i=0; i<2; i++) for (j=0; j<2; j++) {
            v=0; for (k=0; k<2; k++) { t=bb[i][k]; t*=bb[k][j]; t%=MOD; v+=t; v%=MOD; }
            tmx[i][j]=v;
        }
        for (i=0; i<2; i++) for (j=0; j<2; j++) bb[i][j]=tmx[i][j];
        e>>=1;
    }
    r=(mx[0][0]+mx[0][1])%MOD;
    return r;
}

int main() {
    int i;
    LL n;
    scanf("%lld", &n);
    // f[0]=1; f[1]=1; f[2]=2; ==> f[n]-1
    printf("%d\n", (fib(n)-1+MOD)%MOD);
    return 0;
}
0