Problem I. Random Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 392 Accepted Submission(s): 133
Problem Description
There is a positive integer sequence a1,a2,...,an with some unknown positions, denoted by 0. Little Q will replace each 0 by a random integer within the range [1,m]equiprobably. After that, he will calculate the value of this sequence using the following formula :
∏i=1n−3v[gcd(ai,ai+1,ai+2,ai+3)]
Little Q is wondering what is the expected value of this sequence. Please write a program to calculate the expected value.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are 2 integers n,m(4≤n≤100,1≤m≤100) in the first line, denoting the length of the sequence and the bound of each number.
In the second line, there are n integers a1,a2,...,an(0≤ai≤m), denoting the sequence.
In the third line, there are m integers v1,v2,...vm(1≤vi≤109), denoting the array v.
Output
For each test case, print a single line containing an integer, denoting the expected value. If the answer is AB, please print C(0≤C<109+7) where A≡C×B(mod109+7).
Sample Input
2 6 8 4 8 8 4 6 5 10 20 30 40 50 60 70 80 4 3 0 0 0 0 3 2 4
Sample Output
8000 3
Source
2018 Multi-University Training Contest 3
令dp(i,j,k,l)表示到了ai,ai=l,gcd(ai,ai-1)=k,gcd(ai,ai-1,ai-2)=j的答案和
对于当前的i,枚举所有可行的上一个状态即可。
注意点:
对于(j,k,l),如果直接for,显然无用状态太多,我们需要预处理出全部的合法状态,同时预处理出从某一状态转移的下一个状态是什么,这样子就能过题了(果然还是自己的水平不够。。。。。。)
#include <bits/stdc++.h>
#define fir first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const ll mod=1e9+7;
const double eps=1e-7;
const int inf=0x3f3f3f3f;
int n,m;
ll a[105],v[105];
ll gcd(ll a,ll b){
return (b==0?a:gcd(b,a%b));
}
int id[105][105][105];
//给(ai,gcd(ai,ai-1),gcd(ai,ai-1,ai-2))标注id
//这操作 学到了.....
int Gcd[105][105];
ll w[2050][105];
ll g[2050][105];
ll dp[105][2050];
ll qpow(ll a,ll b,ll p){
ll res=1;
while (b){
if(b&1) res=res*a%p;
b>>=1;
a=a*a%p;
}
return res;
}
int main(){
int t;
scanf("%d",&t);
for (int i=1;i<=100;i++){
for (int j=1;j<=100;j++){
Gcd[i][j]=gcd(i,j);
}
}
while (t--){
scanf("%d %d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for (int i=1;i<=m;i++){
scanf("%lld",&v[i]);
}
int cnt=0;
for (int i=1;i<=m;i++){
for (int j=i;j<=m;j+=i){
for (int k=j;k<=m;k+=j){
id[i][j][k]=cnt++;
//k代表ai,j代表gcd(ai,ai-1).i表示gcd(ai,ai-1,ai-2)
//显然,k一定为j倍数,j一定为i倍数
}
}
}
for (int i=1;i<=m;i++){
for (int j=i;j<=m;j+=i){
for (int k=j;k<=m;k+=j){
int x=id[i][j][k];
for (int l=1;l<=m;l++){
//枚举a(i+1)
g[x][l]=id[Gcd[j][l]][Gcd[k][l]][l];
//记录在x状态下,a(i+1)为l的下一个状态
w[x][l]=v[Gcd[i][l]];
//记录v[gcd(ai+1,ai,ai-1,ai-2)]的值
}
}
}
}
for (int i=1;i<=n;i++){
for (int j=0;j<=cnt;j++){
dp[i][j]=0;
//初始化
}
}
int cccc=0;
for (int i=1;i<=m;i++){
if (a[1]&&i!=a[1]) continue;
for (int j=1;j<=m;j++){
if (a[2]&&j!=a[2]) continue;
for (int k=1;k<=m;k++){
if (a[3]&&k!=a[3]) continue;
int J=Gcd[j][k];
int I=Gcd[J][i];
dp[3][id[I][J][k]]=(dp[3][id[I][J][k]]+1)%mod;
//枚举前三个的所有可能,然后赋值为1
}
}
}
// cout<<"cnt="<<cnt<<endl;
// cout<<dp[3][id[2][2][2]]<<" 1111"<<endl;
//cout<<"g24="<<g[id[2][2][2]][4]<<endl;
for (int i=3;i<n;i++){
for (int j=0;j<cnt;j++){
if (!dp[i][j]) continue;
for (int k=1;k<=m;k++){
if (a[i+1]&&a[i+1]!=k) continue;
cccc++;
dp[i+1][g[j][k]]=(dp[i+1][g[j][k]]+w[j][k]*dp[i][j]%mod)%mod;
//cout<<"temp="<<w[j][k]*dp[i][j]%mod<<endl;
}
}
}
// cout<<"num="<<cccc<<endl;
//cout<<"dp2224="<<dp[4][21]<<endl;
ll res=0;
for (int i=0;i<=cnt;i++){
res=(res+dp[n][i])%mod;
}
int num0=0;
for (int i=1;i<=n;i++){
num0+=(a[i]==0);
}
ll temp=qpow(m,num0,mod);
ll inv=qpow(temp,mod-2,mod);
// cout<<res<<" "<<inv<<endl;
res=res*inv%mod;
printf("%lld\n",res);
}
return 0;
}