結果

問題 No.2324 Two Countries within UEC
ユーザー HIcoder
提出日時 2023-05-28 14:35:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,019 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 85,032 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-27 02:05:24
合計ジャッジ時間 8,222 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<tuple>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;

/*
tuple<ll,ll,ll> extgcd(ll a,ll b){
    if(b==0){

        return make_tuple(a,1,0);
    }else{
        auto [g,x,y]=extgcd(b,a%b);

        return make_tuple(g,y,x-(a/b)*y);
    }
}

mod_inv(ll a,ll p){
    extgcd(a,p);

}
*/
 
 
 
//拡張ユークリッド互除法
long long int ext_gcd(long long int a, long long int b, long long int &x, long long int &y)
{
  if (b == 0)
  {
    x = 1;
    y = 0;
    return a;
  }
  long long int q = a / b;
  long long int g = ext_gcd(b, a-q*b, x, y);
  long long int z = x-q*y;
  x = y;
  y = z;
  return g;
}
 
//aとmは互いに素, a^(-1) mod m
long long int modinv(long long int a, long long int m) 
{
  long long int x, y;
  ext_gcd(a, m, x, y);
  x %= m;
  if (x < 0)
    x += m;
  return x;
}

ll gcd(ll x,ll y){
    return (y==0?x:gcd(y,x%y));
}
 
 

int main(){
    ll N,M,P,Q;
    cin>>N>>M>>P>>Q;

    vector<ll> ans(Q);
    ll xi,fi;
    for(int q=0;q<Q;q++){
        cin>>xi>>fi;

        //xiのmod Pでの逆元
        if(gcd(xi,P)!=1){
            //cout<<"tt"<<endl;
            ans[q]=0;


        }else{
            ll invxi=modinv(xi,P);

            //cout<<"(invxi*xi)%P="<<(invxi*xi)%P<<endl;
            ll tmp=fi*invxi%P;

            /*
            ll n=0;

            if((M-tmp)%P==0){
                n=(M-tmp)/P+1;
            }else{

            }
            */
            if(M-tmp<0){
                ans[q]=0;
            }else{
                //
                ll n;
                if(tmp==0){
                    n=(M-tmp)/P;
                }else{
                    n=(M-tmp)/P+1;
                }
                ans[q]=n;

            }
            }

    }
    for(ll v:ans){
        cout<<v<<endl;
    }
}
0