結果

問題 No.613 Solitude by the window
ユーザー rickythetarickytheta
提出日時 2017-12-13 21:24:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,620 ms / 2,000 ms
コード長 2,452 bytes
コンパイル時間 1,623 ms
コンパイル使用メモリ 144,420 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 02:23:26
合計ジャッジ時間 12,179 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1,619 ms
4,376 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 190 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 56 ms
4,376 KB
testcase_08 AC 50 ms
4,380 KB
testcase_09 AC 386 ms
4,380 KB
testcase_10 AC 31 ms
4,380 KB
testcase_11 AC 1,619 ms
4,376 KB
testcase_12 AC 1,620 ms
4,380 KB
testcase_13 AC 1,617 ms
4,376 KB
testcase_14 AC 4 ms
4,384 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 397 ms
4,376 KB
testcase_22 AC 398 ms
4,380 KB
testcase_23 AC 399 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
  // montgomery
  r2 = r%m*r%m;
  mm = calc_mm();
  m4 = MR(r2 * 4);

  // solve
  if(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;
}
0