結果

問題 No.344 ある無理数の累乗
ユーザー ミドリムシミドリムシ
提出日時 2018-04-29 12:46:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,549 bytes
コンパイル時間 1,410 ms
コンパイル使用メモリ 87,848 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 08:00:30
合計ジャッジ時間 3,193 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

#define YES(condition) if(condition) cout << "YES" << endl; else cout << "NO" << endl
#define Yes(condition) if(condition) cout << "Yes" << endl; else cout << "No" << endl
#define POSS(condition) if(condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl
#define Poss(condition) if(condition)cout << "Possible" << endl; else cout << "Impossible" << endl
#define First(condition) if(condition)cout << "First" << endl; else cout << "Second" << endl
#define negative(condition, num) if(condition)cout << -1 << endl; else cout << num << endl
long power(long base, long exponent, long module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position move_pattern[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++){ ans += to_string(*i) + " "; } ans.pop_back(); cout << ans << endl; }
long gcd(long a, long b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }}
#define mod long(1e9 + 7)
#define bitcount(n) __builtin_popcount(n)

struct matrix{
    vector<vector<int>> space;
    
    matrix(vector<vector<int>> element){
        space = element;
    }
    
    matrix(int size){
        space.resize(size, vector<int>(size));
    }
    
    matrix operator *(matrix another){
        matrix ans(int(space.size()));
        for(int i = 0; i < space.size(); i++){
            for(int j = 0; j < space.size(); j++){
                ans.space[i][j] = 0;
                for(int k = 0; k < space.size(); k++){
                    ans.space[i][j] += space[i][k] * another.space[k][j];
                }
                ans.space[i][j] %= 1000;
            }
        }
        return ans;
    }
    
    inline vector<int> operator [](int index){
        return space[index];
    }
};

matrix A({{2, 2}, {1, 0}}), E({{1, 0}, {0, 1}});

matrix matrix_power(int exponent){
    if(exponent % 2){
        return A * matrix_power(exponent - 1);
    }else if(exponent){
        matrix root_ans = matrix_power(exponent / 2);
        return root_ans * root_ans;
    }else{
        return E;
    }
}

int main(){
    int n;
    cin >> n;
    cout << ((matrix_power(n) * matrix({{2, 2}, {2, 999}}))[1][0] + 1000 - (n + 1) % 2) % 1000 << endl;
}
0