結果

問題 No.2324 Two Countries within UEC
ユーザー Naoto FujiwaraNaoto Fujiwara
提出日時 2023-05-29 18:40:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 1,977 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 82,932 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 06:23:37
合計ジャッジ時間 8,263 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 258 ms
4,380 KB
testcase_01 AC 278 ms
4,376 KB
testcase_02 AC 49 ms
4,380 KB
testcase_03 AC 206 ms
4,384 KB
testcase_04 AC 237 ms
4,380 KB
testcase_05 AC 51 ms
4,376 KB
testcase_06 AC 94 ms
4,380 KB
testcase_07 AC 43 ms
4,376 KB
testcase_08 AC 201 ms
4,380 KB
testcase_09 AC 208 ms
4,380 KB
testcase_10 AC 276 ms
4,384 KB
testcase_11 AC 269 ms
4,380 KB
testcase_12 AC 83 ms
4,376 KB
testcase_13 AC 83 ms
4,376 KB
testcase_14 AC 49 ms
4,376 KB
testcase_15 AC 43 ms
4,376 KB
testcase_16 AC 186 ms
4,384 KB
testcase_17 AC 117 ms
4,376 KB
testcase_18 AC 53 ms
4,376 KB
testcase_19 AC 275 ms
4,380 KB
testcase_20 AC 36 ms
4,376 KB
testcase_21 AC 170 ms
4,380 KB
testcase_22 AC 221 ms
4,380 KB
testcase_23 AC 70 ms
4,380 KB
testcase_24 AC 183 ms
4,380 KB
testcase_25 AC 278 ms
4,380 KB
testcase_26 AC 196 ms
4,380 KB
testcase_27 AC 201 ms
4,380 KB
testcase_28 AC 192 ms
4,376 KB
testcase_29 AC 191 ms
4,376 KB
testcase_30 AC 193 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//AC
#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;

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

        }else{
            fi/=g;
            xi/=g;
            ll p=P/g;
           
            ll invxi=modinv(xi,p);

            ll tmp=fi*invxi%p;
            //cout<<"tmp="<<tmp<<endl;

            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