結果

問題 No.1025 Modular Equation
ユーザー 37zigen37zigen
提出日時 2020-04-11 05:45:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,515 ms / 5,000 ms
コード長 4,743 bytes
コンパイル時間 2,384 ms
コンパイル使用メモリ 78,596 KB
実行使用メモリ 60,304 KB
最終ジャッジ日時 2023-10-17 10:51:56
合計ジャッジ時間 56,678 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,504 KB
testcase_01 AC 53 ms
53,504 KB
testcase_02 AC 60 ms
53,516 KB
testcase_03 AC 96 ms
55,420 KB
testcase_04 AC 94 ms
55,452 KB
testcase_05 AC 72 ms
54,584 KB
testcase_06 AC 67 ms
54,584 KB
testcase_07 AC 69 ms
54,588 KB
testcase_08 AC 71 ms
52,696 KB
testcase_09 AC 2,740 ms
59,392 KB
testcase_10 AC 3,303 ms
59,092 KB
testcase_11 AC 2,428 ms
59,016 KB
testcase_12 AC 2,126 ms
58,972 KB
testcase_13 AC 1,994 ms
58,940 KB
testcase_14 AC 1,859 ms
57,400 KB
testcase_15 AC 1,899 ms
58,920 KB
testcase_16 AC 1,691 ms
59,216 KB
testcase_17 AC 1,704 ms
59,068 KB
testcase_18 AC 1,650 ms
59,132 KB
testcase_19 AC 1,791 ms
59,432 KB
testcase_20 AC 2,059 ms
59,804 KB
testcase_21 AC 1,841 ms
58,920 KB
testcase_22 AC 2,092 ms
59,104 KB
testcase_23 AC 2,084 ms
59,836 KB
testcase_24 AC 3,515 ms
59,176 KB
testcase_25 AC 2,369 ms
59,168 KB
testcase_26 AC 2,342 ms
59,140 KB
testcase_27 AC 1,801 ms
59,736 KB
testcase_28 AC 1,764 ms
59,448 KB
testcase_29 AC 1,796 ms
59,304 KB
testcase_30 AC 1,978 ms
60,096 KB
testcase_31 AC 1,851 ms
60,304 KB
testcase_32 AC 2,350 ms
59,316 KB
testcase_33 AC 53 ms
53,516 KB
testcase_34 AC 326 ms
60,008 KB
権限があれば一括ダウンロードができます

ソースコード

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;
		--n;
	}
}

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}
	
	final long MOD=(long)1e9+7;
	
	long ADD(long a,long b) {
		return a+b>=MOD?a+b-MOD:a+b;
	}
	
	long gcd(long a,long b) {
		return a==0?b:gcd(b%a,a);
	}
	
	long pow_mod(long a,long n,long p) {
		long ret=1;
		for (;n>0;n>>=1,a=a*a%p) if(n%2==1) ret=ret*a%p;
		return ret;
	}
	
	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();
		int n=sc.nextInt();
		long[] a=new long[n];
		long k=gcd(sc.nextInt(),p-1);
		long b=sc.nextLong();
		long g=primitive_root(p);
		long[] gk=new long[(int)((p-1)/k)+1];
		gk[0]=1;gk[1]=pow_mod(g, k, p);
		for (int i=2;i<gk.length;++i) gk[i]=gk[i-1]*gk[1]%p;
		long[][] dp=new long[2][(int)p];
		int[] ds_root=new int[(int)p];
		DJSet ds=new DJSet((int)p);
		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*gk[1]%p));
		int[] list=new int[ds.n];
		{
			int pos=0;
			for (int i=0;i<p;++i) if (ds.root(i)==i) list[pos++]=i;
		}
		for (int i=0;i<p;++i) ds_root[i]=ds.root(i);
		for (int i=1;i<=n;++i) {
			for (int j=0;j<p;++j) dp[i%2][j]=dp[(i-1)%2][j];
			if (a[i-1]==0) {
				for (int j=0;j<p;++j) dp[i%2][j]=(dp[(i-1)%2][j]*(p-1)+dp[i%2][j])%MOD;
				continue;
			}
			for (int to:list) {
				for (int j=0;j<(p-1)/k;++j) {
					int from=(int)((to+(p-a[i-1])*gk[j])%p);
					if (dp[(i-1)%2][ds_root[from]]==0) continue;
					dp[i%2][ds_root[to]]=(dp[i%2][ds_root[to]]+dp[(i-1)%2][ds_root[from]]*k)%MOD;
				}
			}
		}
		pw.println(dp[n%2][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