結果

問題 No.1025 Modular Equation
ユーザー chocoruskchocorusk
提出日時 2020-03-04 22:50:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,287 ms / 5,000 ms
コード長 1,396 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 71,168 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2023-08-20 19:38:22
合計ジャッジ時間 24,256 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 767 ms
4,464 KB
testcase_10 AC 790 ms
4,780 KB
testcase_11 AC 789 ms
4,488 KB
testcase_12 AC 857 ms
4,656 KB
testcase_13 AC 767 ms
4,440 KB
testcase_14 AC 832 ms
4,616 KB
testcase_15 AC 883 ms
4,640 KB
testcase_16 AC 862 ms
4,500 KB
testcase_17 AC 884 ms
4,584 KB
testcase_18 AC 815 ms
4,532 KB
testcase_19 AC 923 ms
5,400 KB
testcase_20 AC 881 ms
5,056 KB
testcase_21 AC 908 ms
4,932 KB
testcase_22 AC 912 ms
4,876 KB
testcase_23 AC 1,209 ms
6,076 KB
testcase_24 AC 842 ms
4,564 KB
testcase_25 AC 871 ms
4,556 KB
testcase_26 AC 861 ms
4,516 KB
testcase_27 AC 921 ms
4,572 KB
testcase_28 AC 924 ms
4,704 KB
testcase_29 AC 934 ms
4,588 KB
testcase_30 AC 950 ms
5,184 KB
testcase_31 AC 1,049 ms
5,388 KB
testcase_32 AC 1,287 ms
6,144 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 813 ms
4,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;
const ll MOD=1e9+7;
ll powmod(ll a, ll k, ll mod){
	if(a==0) return 0;
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=mod;
        }
        ap=ap*ap;
        ap%=mod;
        k>>=1;
    }
    return ans;
}
ll inv(ll a, ll mod){
    return powmod(a, mod-2, mod);
}
ll gcd(ll a, ll b){
	if(b==0) return a;
	return gcd(b, a%b);
}
ll a[505], q[100010], dp[2][100010];
int qi[100010];
int main()
{
    int n, k; ll b, p;
	cin>>p>>n>>k>>b;
	k=gcd(k, p-1);
	for(int i=0; i<n; i++) cin>>a[i];
	ll r;
	for(r=1; r<p; r++){
		bool dame=0;
		ll pp=1;
		for(int i=0; i<p-2; i++){
			(pp*=r)%=p;
			if(pp==1){
				dame=1; break;
			}
		}
		if(!dame) break;
	}
	q[0]=1;
	for(int i=1; i<p-1; i++) q[i]=q[i-1]*r%p;
	for(int i=0; i<p-1; i++) qi[q[i]]=i;
	dp[0][k]=1;
	for(int i=0; i<n; i++){
		for(int j=0; j<=k; j++) dp[(i&1)^1][j]=dp[i&1][j];
		for(int j=0; j<k; j++){
			for(int l=0; l<(p-1)/k; l++){
				ll m=(q[l*k]*a[i]+q[j])%p;
				if(m==0) (dp[(i&1)^1][k]+=dp[i&1][j]*k)%=MOD;
				else (dp[(i&1)^1][qi[m]%k]+=dp[i&1][j]*k)%=MOD;
			}
		}
		if(a[i]==0) (dp[(i&1)^1][k]+=dp[i&1][k]*(p-1))%=MOD;
		else (dp[(i&1)^1][qi[a[i]]%k]+=dp[i&1][k]*(p-1))%=MOD;
	}
	if(b==0) cout<<dp[n&1][k]<<endl;
	else cout<<dp[n&1][qi[b]%k]*inv((p-1)/k, MOD)%MOD<<endl;
    return 0;
}
0