結果
問題 | No.613 Solitude by the window |
ユーザー |
![]() |
提出日時 | 2017-12-13 21:24:21 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 1,623 ms / 2,000 ms |
コード長 | 2,452 bytes |
コンパイル時間 | 1,485 ms |
コンパイル使用メモリ | 159,892 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-12-14 10:11:36 |
合計ジャッジ時間 | 10,719 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
#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)ll 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 + ((x * mm)&rmsk)*m) >> rsh;return t<m ? t : t-m;}int floyd_cycle_algorithm(ll init){int mu = 0;memo[0] = init;init = MR(r2 * init);ll x = init;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);}ll solve(){if(m==2)return 0ll;// montgomeryr2 = r%m*r%m;mm = calc_mm();m4 = MR(r2 * 4);// solveif(n <= LAMBDA){return naive(2,n);}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);return x;}int main(){// while(true){// m = rand()%1024;// if(m<=1)continue;// bool ok = true;// for(int x=2;x*x<=m;x++)if(m%x==0){// ok = false;break;// }// if(!ok)continue;// ll v = 2;// FOR(i,1,10000){// n = i;// v = v * (v+4) % m;// ll x = solve();// if(v != x){// puts("WRONG");// printf("n = %lld, m = %lld\n", (ll)i, m);// printf("my solver : %lld\n", x);// printf("naive : %lld\n", v);// exit(0);// }// }// }cin>>n>>m;cout<<solve()<<endl;return 0;}