結果

問題 No.344 ある無理数の累乗
ユーザー はまやんはまやん
提出日時 2017-04-22 13:12:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,692 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 175,384 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-21 16:05:48
合計ジャッジ時間 2,920 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
int mod = 1000;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
typedef long long ll;
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b)
{
int n = b.size(); Vec ret(n, 0);
rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
return ret;
}
Mat mulMatMat(Mat a, Mat b)
{
int n = a.size(); Mat ret(n, Vec(n, 0));
rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
Mat fastpow(Mat x, ll n)
{
Mat ret(x.size(), Vec(x.size(), 0));
rep(i, 0, x.size()) ret[i][i] = 1;
while (0 < n) { if ((n % 2) == 0) { x = mulMatMat(x, x); n >>= 1; } else { ret = mulMatMat(ret, x); --n; } }
return ret;
}
void printVec(Vec a) { cout << "[ "; rep(i, 0, a.size()) cout << a[i] << " "; cout << "]" << endl; }
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
//-----------------------------------------------------------------------------------
int main() {
int N; cin >> N;
if (N == 0) {
printf("1\n");
return 0;
}
Mat ma(2, Vec(2, 0));
ma[0][0] = ma[0][1] = 2;
ma[1][0] = 1;
Vec ve(2, 0);
ve[0] = ve[1] = 2;
ma = fastpow(ma, N - 1);
ve = mulMatVec(ma, ve);
int ans = ve[0];
if (N % 2 == 0) ans = (ans - 1 + 1000) % 1000;
cout << ans << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0