結果
| 問題 | No.613 Solitude by the window |
| コンテスト | |
| ユーザー |
rickytheta
|
| 提出日時 | 2017-12-13 11:33:09 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,632 bytes |
| 記録 | |
| コンパイル時間 | 1,621 ms |
| コンパイル使用メモリ | 157,688 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-14 09:12:12 |
| 合計ジャッジ時間 | 14,185 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 TLE * 1 |
| other | AC * 19 TLE * 2 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef 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 31
#define r (1ll<<31)
#define rmsk (r-1ll)
ll n,m;
ll r2, mm;
ll m4;
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 + ((x * mm) & rmsk)*m) >> rsh;
return t<m ? t : t-m;
}
#define f(x) ((x)*((x)+4ll)%m)
int floyd_cycle_algorithm(ll init){
int mu = 0;
init = MR(r2 * init);
ll x = init;
do {
mu++;
x = MR(x*(x+m4));
}while(x!=init);
return mu;
}
ll naive(ll x, ll n){
x = MR(r2 * x);
while(n--){
x = MR(x*(x+m4));
}
return MR(x);
}
int main(){
cin>>n>>m;
if(m == 2){
cout<<0<<endl;
return 0;
}
// montgomery
r2 = r*r%m;
mm = calc_mm();
m4 = MR(r2 * 4);
// solve
if(n < 100){
cout<<naive(2,n)<<endl;
return 0;
}
ll x = naive(2,100);
n -= 100;
int mu = floyd_cycle_algorithm(x);
n %= mu;
x = naive(x,n);
cout<<x<<endl;
return 0;
}
rickytheta