結果

問題 No.186 中華風 (Easy)
ユーザー 37zigen37zigen
提出日時 2020-03-27 05:05:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 4,890 bytes
コンパイル時間 2,734 ms
コンパイル使用メモリ 76,888 KB
実行使用メモリ 84,760 KB
最終ジャッジ日時 2023-09-27 00:59:49
合計ジャッジ時間 11,645 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 331 ms
82,916 KB
testcase_01 AC 328 ms
82,880 KB
testcase_02 AC 337 ms
82,708 KB
testcase_03 AC 331 ms
83,084 KB
testcase_04 AC 326 ms
82,920 KB
testcase_05 AC 330 ms
82,576 KB
testcase_06 AC 329 ms
82,832 KB
testcase_07 AC 330 ms
82,544 KB
testcase_08 AC 336 ms
83,224 KB
testcase_09 AC 338 ms
83,008 KB
testcase_10 AC 333 ms
82,556 KB
testcase_11 AC 332 ms
82,892 KB
testcase_12 AC 332 ms
80,948 KB
testcase_13 AC 334 ms
82,768 KB
testcase_14 AC 332 ms
82,828 KB
testcase_15 AC 326 ms
84,760 KB
testcase_16 AC 330 ms
83,096 KB
testcase_17 AC 327 ms
83,232 KB
testcase_18 AC 326 ms
82,800 KB
testcase_19 AC 328 ms
82,740 KB
testcase_20 AC 327 ms
80,536 KB
testcase_21 AC 326 ms
82,820 KB
testcase_22 AC 328 ms
82,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		new Main().solve();
	}
	long gcd(long t1,long t2){
		if(t1<t2){
			long d=t2;
			t2=t1;
			t1=d;
		}
		if(t2==0)return t1;
		return gcd(t2,t1%t2);
	}
	//x[i]=Y mod[i]
	boolean hasContradiction(long[] x,long[] mod){
		int n=x.length;
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				long g=gcd(mod[i],mod[j]);
				if((x[i]-x[j])%g!=0)return true;
			}
		}
		return false;
	}

	void solve(){
		Prime p=new Prime();
		ArrayList<Integer> primeList=p.primeList((int)1e7);
		Scanner sc=new Scanner(System.in);
		int n=3;
		long[] x=new long[n];//x余る
		long[] y=new long[n];//mod y
		HashMap<Long,long[]> map=new HashMap<Long,long[]>();
		/*
		 * HashMap<f.base,{f.exp,x}
		 */
		for(int i=0;i<n;i++){
			x[i]=sc.nextLong();
			y[i]=sc.nextLong();
			ArrayList<Factor> f=p.primeFactorF(primeList, y[i]);

			for(Factor ff:f){
				long base=ff.base;
				long exp=ff.exp;
				if(!map.containsKey(base)){
//					System.out.println(base+" "+exp+" "+x[i]);
					map.put(base, new long[]{exp,x[i]});
				}
				else{
					if(map.get(base)[0]<exp){
						map.put(base,new long[]{exp,x[i]});
					}
				}
			}
		}
		if(hasContradiction(x,y)){
			System.out.println(-1);
			return;
		}
		long[] X=new long[map.size()];
		long[] mod=new long[map.size()];
		int now=0;
		boolean zero=true;
		for(Map.Entry<Long,long[]> e:map.entrySet()){
//			System.out.println(e.getKey()+" "+e.getValue()[0]);
			mod[now]=pow(e.getKey(),e.getValue()[0]);
			X[now]=e.getValue()[1]%mod[now];
			if(X[now]!=0)zero=false;
			now++;
		}
		if(zero){
			long ans=1;
			for(int i=0;i<mod.length;i++){
				ans*=mod[i];
			}
			System.out.println(ans);
			return;
		}
		long ans=garner(X,mod);
		System.out.println(ans);
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
	long pow(long a,long n){
		long A=a;
		long ans=1;
		while(n>=1){
			if(n%2==0){
				A=A*A;
				n/=2;
			}else if(n%2==1){
				ans=ans*A;
				n--;
			}
		}
		return ans;
	}


	long garner(long[] x,long[] m){
		assert x.length==m.length;
		int n=x.length;
		/**
		 * gamma[i]=(m[0]m[1]...m[i-1])^(-1) mod m[i]
		 */
		long[] gamma=new long[n];
		for(int i=0;i<n;i++){
			long prod=1;
			for(int j=0;j<i;j++){
				prod=prod*m[j]%m[i];
			}
			//			gamma[i]=inv_pos(prod,m[i]);
			gamma[i]=inv(prod,m[i]);
		}
		/**
		 * まずv_iを求める。
		 * Y(now)はmod M[k]でX[k]となる数。(0<=k<=i-1)
		 * Y(now)+M[0]*M[1]*...*M[i-1]v_i =x[i] mod m[i] 
		 * (d=M[0]*M[1]*M[2]*M[3]*...*M[i-1]とすると)
		 * Y(now)+d*v_i=x[i] mod m[i]
		 * v_i=gamma[i]*(x[i]-Y(now)) mod m[i]
		 *
		 * Y(now)=Y(now)+d*v_i (mod M[0][M[1]M[2]...M[i-1]M[i])
		 */
		/**
		 * Y(i-1)=((V[i-1]*m[i-2]+v[i-2])*m[i-3]+v[i-3])*m[i-4+v[i-4]....
		 */

		long[] v=new long[n];
		v[0]=x[0];
		for(int i=1;i<n;i++){
			long tmp=v[i-1];
			for(int j=i-2;j>=0;j--){
				tmp=(tmp*m[j]+v[j])%m[i];
			}
			v[i]=(x[i]-tmp)*gamma[i]%m[i];
			while(v[i]<0)v[i]+=m[i];
		}
		long ret=0;
		for(int i=v.length-1;i>=0;i--){
			ret=ret*m[i]+v[i];
		}
		return ret;
	}
	/**
	 * ax = 1 mod m
	 * ax + my = 1を満たす x を求めたい。
	 *
	 * a*1 + m*0 = a;
	 * a*0 + m*1 = m;
	 *
	 * a*p + m*q =
	 */
	long inv(long a,long mod){
		long b = mod;
		long p = 1, q =0;
		while(b > 0){
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		return p<0 ? p + mod: p;
	}
	class Prime{
		boolean[] isPrimeArray(int max){
			boolean[] isPrime=new boolean[max+1];
			Arrays.fill(isPrime, true);
			isPrime[0]=isPrime[1]=false;
			for(int i=2;i*i<=max;i++){
				if(isPrime[i]){
					for(int j=2;j*i<=max;j++){
						isPrime[j*i]=false;
					}
				}
			}
			return isPrime;
		}
		/*
		 * max以下の素数のリストを返す
		 */
		ArrayList<Integer> primeList(int max){
			boolean[] isPrime=isPrimeArray(max);
			ArrayList<Integer> primeList=new ArrayList<Integer>();
			for(int i=2;i<=max;i++){
				if(isPrime[i]){
					primeList.add(i);
				}
			}
			return primeList;
		}
		/*
		 * numをprimeListの素数をもとに素因数分解し、因数を
		 * ArrayList<Factor>の形で返す。
		 * primeListにはnumの平方根以下の素数が含まれていなければならない。
		 *
		 */
		ArrayList<Factor> primeFactorF(ArrayList<Integer> primeList,long num){
			ArrayList<Factor> ret=new ArrayList<Factor>();
			for(int p:primeList){
				int exp=0;
				while(num%p==0){
					num/=p;
					exp++;
				}
				if(exp>0)ret.add(new Factor(p,exp));
			}
			if(num>1)ret.add(new Factor((int)num,1));
			return ret;
		}
	}
	class Factor{
		long base,exp;
		Factor(long base,long exp){
			this.base=base;
			this.exp=exp;
		}
	}
	class Pair{
		long x;
		long mod;
		Pair(long x,long mod){
			this.x=x;
			this.mod=mod;
		}
	}
}
0