結果

問題 No.3396 ChRisTmas memory
コンテスト
ユーザー Mike scarf
提出日時 2025-12-12 15:33:32
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 1,156 ms / 4,000 ms
コード長 1,554 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,042 ms
コンパイル使用メモリ 275,936 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-12 15:33:49
合計ジャッジ時間 16,684 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:21:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 |     scanf("%lld",&t);
      |     ~~~~~^~~~~~~~~~~
main.cpp:24:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |         scanf("%lld",&op);
      |         ~~~~~^~~~~~~~~~~~
main.cpp:26:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |             scanf("%lld%lld",&m,&r);
      |             ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:48:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |             scanf("%lld",&k);
      |             ~~~~~^~~~~~~~~~~
main.cpp:53:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   53 |             scanf("%lld",&m);
      |             ~~~~~^~~~~~~~~~~

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=15000;
ll s[maxn+5],q[maxn+5];
ll cnt,lim;
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll egcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1,y=0;
        return a;
    }
    ll d=egcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
int main(){
    ll t,op,m,r,k,i,zc,cur,rhs,lhs,x,y,g,mp,ans;
    scanf("%lld",&t);
    cnt=0,lim=0;
    while(t--){
        scanf("%lld",&op);
        if(op==1){
            scanf("%lld%lld",&m,&r);
            if(lim==cnt){
                zc=0,cur=1;
                for(i=1;i<=cnt;i++){
                    zc=(zc+s[i]*cur)%m;
                    cur=(cur*q[i])%m;
                }
                rhs=(r-zc+m)%m;
                lhs=cur;
                g=gcd(lhs,m);
                if(rhs%g==0){
                    mp=m/g;
                    egcd(lhs/g,mp,x,y);
                    x=(x%mp+mp)%mp;
                    s[cnt+1]=(x*(rhs/g))%mp;
                    q[cnt+1]=mp;
                    lim++;
                }
            }
            cnt++;
        }
        else if(op==2){
            scanf("%lld",&k);
            cnt-=k;
            if(lim>cnt) lim=cnt;
        }
        else{
            scanf("%lld",&m);
            if(lim<cnt) printf("-1\n");
            else{
                ans=0,cur=1;
                for(i=1;i<=cnt;i++){
                    ans=(ans+s[i]*cur)%m;
                    cur=(cur*q[i])%m;
                }
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}
0