結果

問題 No.344 ある無理数の累乗
ユーザー Hachimori
提出日時 2016-02-13 16:51:26
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,078 bytes
コンパイル時間 557 ms
コンパイル使用メモリ 57,108 KB
実行使用メモリ 7,412 KB
最終ジャッジ日時 2024-09-22 06:15:25
合計ジャッジ時間 1,482 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstring>
using namespace std;
const int MOD = 1000;


int n;

void read() {
    cin >> n;
}


void work() {
    if (n == 0) {
        cout << 1 << endl;
        return;
    }
    
    if (n == 1) {
        cout << 2 << endl;
        return;
    }
    
    static int visited[MOD][MOD];
    memset(visited, -1, sizeof(visited));

    
    visited[2][2] = 0;
    int pre1 = 2;
    int pre2 = 2;
    
    for (int i = 2; i < n; ++i) {
        int cur = 2 * (pre1 + pre2) % MOD;

        if (visited[cur][pre1] >= 0) {
            int cycle = i - visited[cur][pre1];
            
            for (int j = 0; j < (n - i) % cycle; ++j) {
                cur = 2 * (pre1 + pre2) % MOD;
                pre2 = pre1;
                pre1 = cur;
            }
            break;
        }
        
        visited[cur][pre1] = i;
        pre2 = pre1;
        pre1 = cur;
    }

    int ans = 2 * (pre1 + pre2) % MOD;
    if (n % 2 == 0) {
        ans = (ans + 999) % MOD;
    }
    cout << ans << endl;
}


int main() {
    read();
    work();
    return 0;
}
0