結果

問題 No.1025 Modular Equation
ユーザー 37zigen37zigen
提出日時 2020-04-11 03:10:55
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,443 bytes
コンパイル時間 2,454 ms
コンパイル使用メモリ 79,248 KB
実行使用メモリ 446,620 KB
最終ジャッジ日時 2024-09-17 05:28:27
合計ジャッジ時間 47,468 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,596 KB
testcase_01 AC 55 ms
36,580 KB
testcase_02 AC 79 ms
37,424 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 100 ms
38,528 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 3,957 ms
428,092 KB
testcase_22 WA -
testcase_23 AC 3,828 ms
440,944 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 4,051 ms
436,732 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

class DJSet {
	int n;
	int[] upper;
	
	public DJSet(int n) {
		this.n=n;
		upper=new int[n];
		Arrays.fill(upper, -1);
	}
	
	int root(int x) {
		return upper[x]<0?x:(upper[x]=root(upper[x]));
	}
	
	boolean equiv(int x,int y) {
		return root(x)==root(y);
	}
	
	void setUnion(int x,int y) {
		x=root(x);y=root(y);
		if (x==y) return;
		if (upper[x]<upper[y]) {
			x^=y;y^=x;x^=y;
		}
		if (upper[x]==upper[y]) --upper[y];
		upper[x]=y;
	}
}

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}
	
	final long MOD=(long)1e9+7;
	
	long gcd(long a,long b) {
		return a==0?b:gcd(a%b,a);
	}
	
	long pow_mod(long a,long n,long p) {
		return n==0?1:(pow_mod(a*a%p,n/2,p)*(n%2==1?a:1))%p;
	}
	
	long primitive_root(long p) {
		if (p==2) return 1;
		out:for (long root=2;root<p;++root) {
			for (long div=2;div*div<=p-1;++div) {
				if ((p-1)%div!=0) continue;
				for (long d:new long[] {div,(p-1)/div}) {
					if (pow_mod(root,d,p)==1) continue out;
				}
			}
			return root;
		}
		throw new AssertionError();
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		PrintWriter pw=new PrintWriter(System.out);
		long p=sc.nextLong();
		long g=primitive_root(p);
		int n=sc.nextInt();
		long[] a=new long[n];
		long k=sc.nextInt();
		long b=sc.nextLong();
		DJSet ds=new DJSet((int)p);
		long[][] dp=new long[n+1][(int)p];
		k=gcd(k,p-1);
		dp[0][0]=1;
		for (int i=0;i<n;++i) a[i]=sc.nextLong();

		for (int i=1;i<p;++i) {
			ds.setUnion(i, (int)(i*pow_mod(g, k, p)%p));
		}
		ArrayList<Integer> list=new ArrayList<>();
		for (int i=1;i<p;++i) if (ds.root(i)==i) list.add(i);
		list.add(0);
		long gk=pow_mod(g, k, p);
		for (int i=1;i<=n;++i) {
			if (a[i-1]==0) {
				for (int j=0;j<p;++j) dp[i][j]=dp[i-1][j]*(p-1)%MOD;
				continue;
			}
			for (int j=0;j<p;++j) dp[i][j]=dp[i-1][j];
			for (int to:list) {
				for (long add=1,j=0;j<(p-1)/k;++j,add=add*gk%p) {
					int from=(int)((to-a[i-1]*add%p+p)%p);
					dp[i][ds.root(to)]+=dp[i-1][ds.root(from)]*k%MOD;
					dp[i][ds.root(to)]%=MOD;
				}
			}
		}
		pw.println(dp[n][ds.root((int)b)]);
		pw.close();
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    
    public double nextDouble() { return Double.parseDouble(next());}
}
0