結果

問題 No.187 中華風 (Hard)
ユーザー 37zigen37zigen
提出日時 2016-05-16 17:58:40
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,303 bytes
コンパイル時間 3,717 ms
コンパイル使用メモリ 82,736 KB
実行使用メモリ 46,024 KB
最終ジャッジ日時 2024-04-15 22:31:56
合計ジャッジ時間 15,201 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 334 ms
44,356 KB
testcase_01 AC 334 ms
44,768 KB
testcase_02 AC 564 ms
45,640 KB
testcase_03 AC 557 ms
45,884 KB
testcase_04 AC 583 ms
45,816 KB
testcase_05 AC 575 ms
45,980 KB
testcase_06 AC 568 ms
45,644 KB
testcase_07 AC 575 ms
45,696 KB
testcase_08 AC 434 ms
44,336 KB
testcase_09 AC 472 ms
45,404 KB
testcase_10 AC 466 ms
45,916 KB
testcase_11 AC 585 ms
45,756 KB
testcase_12 AC 575 ms
45,784 KB
testcase_13 AC 338 ms
44,476 KB
testcase_14 AC 400 ms
45,592 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 133 ms
41,528 KB
testcase_18 AC 305 ms
44,020 KB
testcase_19 AC 130 ms
41,740 KB
testcase_20 AC 471 ms
43,904 KB
testcase_21 AC 131 ms
41,820 KB
testcase_22 AC 568 ms
45,516 KB
testcase_23 WA -
testcase_24 AC 131 ms
42,056 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;
    }
    long MOD=1_000_000_000+7;
    void solve(){
        Prime p=new Prime();
        ArrayList<Integer> primeList=p.primeList((int)Math.sqrt(MOD));
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        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){
                if(!f.contains(ff.base))
                    map.put(ff.base, new long[]{ff.exp,x[i]});
                else{
                    if(map.get(ff.base)[0]<ff.exp)
                        map.put(ff.base,new long[]{ff.exp,y[i]});
                }
            }
        }
        long[] X=new long[map.size()];
        long[] mod=new long[map.size()];
        int now=0;
        for(Map.Entry<Long,long[]> e:map.entrySet()){
            mod[now]=pow(e.getKey(),e.getValue()[0]);
            X[now]=e.getValue()[1]%mod[now];
            now++;
        }
        if(hasContradiction(x,y)){
            System.out.println(-1);
            return;
        }
        long ans=garner(X,mod);
        if(ans==0){
        	ans=1;
        	for(int i=0;i<mod.length;i++){
        		ans=(ans*mod[i])%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])%MOD;
        }
        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