結果

問題 No.181 A↑↑N mod M
ユーザー chocorusk
提出日時 2019-07-10 09:46:43
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,194 bytes
コンパイル時間 945 ms
コンパイル使用メモリ 105,812 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-14 13:51:46
合計ジャッジ時間 2,164 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘ll solve(ll, ll, ll)’:
main.cpp:48:23: warning: ‘d’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |     return p[d1+(solve(a, n-1, d)-d1%d+d)%d];
      |                  ~~~~~^~~~~~~~~~~
main.cpp:42:28: warning: ‘d1’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   42 |         else return p[d1+(a-d1)%d];
      |                          ~~^~~~

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll solve(ll a, ll n, ll m){
    if(m==1) return 0;
    if(a==1 || n==0) return 1;
    if(n==1) return a%m;
    ll p[2500];
    p[0]=1;
    map<ll, ll> mp;
    mp[1]=0;
    ll d1, d;
    for(int i=1; i<=m; i++){
        p[i]=p[i-1]*a%m;
        if(mp.find(p[i])!=mp.end()){
            d1=mp[p[i]], d=i-d1;
            break;
        }
        mp[p[i]]=i;
    }
    if(n==2){
        if(a<d1) return p[a];
        else return p[d1+(a-d1)%d];
    }else if(n==3 && a<=4){
        if(a==2 && 4<d1) return p[4];
        else if(a==3 && 27<d1) return p[27];
        else if(a==4 && 256<d1) return p[256];
    }
    return p[d1+(solve(a, n-1, d)-d1%d+d)%d];
}
int main()
{
    ll a, n, m;
    cin>>a>>n>>m;
    cout<<solve(a, n, m)<<endl;
    return 0;
}
0