結果
| 問題 |
No.613 Solitude by the window
|
| コンテスト | |
| ユーザー |
rickytheta
|
| 提出日時 | 2017-12-13 21:13:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,947 bytes |
| コンパイル時間 | 1,510 ms |
| コンパイル使用メモリ | 158,848 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-14 10:11:18 |
| 合計ジャッジ時間 | 10,814 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 WA * 4 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))
#define rsh 32
#define r (1ll<<rsh)
#define rmsk (r-1ll)
ll n,m;
ll r2, mm;
ll m4;
#define LAMBDA 512
#define stepsh 17
#define step (1<<stepsh)
#define memosh (32-stepsh)
unsigned memo[1<<memosh];
ll calc_mm(){
ll res = 0;
ll t = 0;
ll rr = r;
ll i = 1;
while(rr > 1){
if(!(t%2)){
t += m;
res += i;
}
t /= 2;
rr /= 2;
i *= 2;
}
return res;
}
inline ll MR(ll x){
ll t = (x + ((unsigned int)(x * mm))*m) >> rsh;
return t<m ? t : t-m;
}
int floyd_cycle_algorithm(ll init){
int mu = 0;
init = MR(r2 * init);
ll x = init;
memo[0] = x;
do {
mu++;
ll x4 = x+m4;
if(x4 >= m) x4 -= m;
x = MR(x*x4);
if((mu&(step-1))==0){
memo[mu>>stepsh] = MR(x);
}
}while(x!=init);
return mu;
}
ll naive(ll x, ll n){
x = MR(r2 * x);
while(n--){
ll x4 = x+m4;
if(x4 >= m) x4 -= m;
x = MR(x*x4);
}
return MR(x);
}
int main(){
cin>>n>>m;
if(m == 2){
cout<<0<<endl;
return 0;
}
// montgomery
r2 = r%m*r%m;
mm = calc_mm();
m4 = MR(r2 * 4);
// solve
if(n <= LAMBDA){
cout<<naive(2,n)<<endl;
return 0;
}
ll x = naive(2,LAMBDA);
n -= LAMBDA;
int mu = floyd_cycle_algorithm(x);
n %= mu;
x = memo[n>>stepsh];
n = n & (step-1);
x = naive(x,n);
cout<<x<<endl;
return 0;
}
rickytheta