結果

問題 No.186 中華風 (Easy)
ユーザー n_vipn_vip
提出日時 2015-05-14 20:11:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 3,090 bytes
コンパイル時間 1,288 ms
コンパイル使用メモリ 109,784 KB
実行使用メモリ 8,160 KB
最終ジャッジ日時 2023-09-27 00:46:09
合計ジャッジ時間 2,399 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,868 KB
testcase_01 AC 19 ms
8,156 KB
testcase_02 AC 18 ms
7,956 KB
testcase_03 AC 19 ms
8,144 KB
testcase_04 AC 19 ms
8,100 KB
testcase_05 AC 19 ms
7,944 KB
testcase_06 AC 19 ms
8,156 KB
testcase_07 AC 19 ms
8,108 KB
testcase_08 AC 19 ms
7,872 KB
testcase_09 AC 19 ms
8,112 KB
testcase_10 AC 18 ms
7,900 KB
testcase_11 AC 18 ms
7,896 KB
testcase_12 AC 18 ms
7,960 KB
testcase_13 AC 18 ms
8,160 KB
testcase_14 AC 19 ms
7,956 KB
testcase_15 AC 19 ms
7,960 KB
testcase_16 AC 18 ms
7,956 KB
testcase_17 AC 19 ms
7,900 KB
testcase_18 AC 19 ms
7,884 KB
testcase_19 AC 16 ms
8,020 KB
testcase_20 AC 16 ms
7,900 KB
testcase_21 AC 16 ms
7,904 KB
testcase_22 AC 17 ms
8,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y-1);(X) >=0;--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()

using namespace std;
typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<class T> using vv=vector<vector<T>>;
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}


const int PRIME_MAX=1123456;
vector<int> primes;
int prime[PRIME_MAX];
void findPrime(){
  for(int i=2;i<PRIME_MAX;i++)if(!prime[i]){
      primes.pb(i);
      for(int j=i*2;j<PRIME_MAX;j+=i)
	prime[j]=1;
  }
}
//extGCD
ll extgcd(ll a,ll b,ll &x,ll &y){
  ll g=a; x=1,y=0;
  if(b){
    g=extgcd(b,a%b,y,x);
    y-=(a/b)*x;
  }
  return g;
}

//中国剰余定理
ll china2(const vector<ll> &a,const vector<ll> &m){
  ll p=1,re=0,pp,x,y;
  rep(i,m.size())
    p*=m[i];
  //cout<<p<<"!!!!!!!"<<endl;
  rep(i,m.size()){
    pp=p/m[i];
    extgcd(m[i],pp,x,y);
    (re+=pp*y%p*a[i]%p)%=p;
  }
  return (re%p+p)%p;
}
int updChina(map<ll,pll> &mp,ll a,ll m,ll p){
  //cout<<a<<","<<m<<","<<p<<endl;
  if(mp.count(p)){
    pll tmp=mp[p];
    if((tmp.X-a)%min(tmp.Y,m))
      return 1;
    if(tmp.Y<m)
      mp[p]=pll(a,m);
  }else{
    mp[p]=pll(a,m);
  }
  return 0;
}
//互いに素じゃなくてもよい。解なしは-1
ll china(const vector<ll> &a,const vector<ll> &m){
  map<ll,pll> mp;  //PRIME -> REM * MOD
  pll tmp;
  ll num,p=1;
  rep(i,a.size()){
    num=m[i];
    rep(j,primes.size())if(m[i]%primes[j]==0){
      p=1;
      while(num%primes[j]==0){
	p*=primes[j];
	num/=primes[j];
      }
      if(updChina(mp,a[i],p,primes[j]))
	return -1;
    }
    if(num>1)
      if(updChina(mp,a[i],num,num))
	return -1;
  }
  //for(auto t:mp)cout<<t<<",";cout<<endl;
  vector<ll> aa,mm;
  for(auto temp:mp){
    num=temp.Y.X; p=temp.Y.Y;
    aa.pb((num%p+p)%p);
    mm.pb(p);
  }
  //cout<<aa<<mm;
  return china2(aa,mm);
}

int main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);
  int i,j,k;
  findPrime();
  int n;
  n=3;//cin>>n;
  vector<ll> a(n),m(n);
  long long te,mp;
  rep(i,n){
    cin>>te>>mp;
    a[i]=te; m[i]=mp;
  }
  ll re=china(a,m),lcm=1;
  rep(i,n)
    lcm*=m[i]/__gcd(lcm,m[i]);
  //  (re+=lcm)%=lcm;
  if(!re)re+=lcm;
  ll d=1e18;
  long long res=re/d,ult=re%d;
  if(res)
    cout<<res<<ult<<endl;
  else
    cout<<ult<<endl;
  return 0;
}
0