比赛 2022级数学专题练习赛8 评测结果 AAAAAAAAAA
题目名称 奇怪的背包 最终得分 100
用户昵称 op_组撒头屯 运行时间 1.192 s
代码语言 C++ 内存使用 17.08 MiB
提交时间 2023-02-06 21:29:42
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define si set<int>
#define unsi unordered_set<int>
#define qi queue<int>
#define sti stack<int>
#define pqi priority_queue<int>
#define mii map<int,int>
#define unmii unordered_map<int,int>
#define fi first
#define se second
#define pb push_back
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
const int N=1000000+5;
const int M=1350+5;
const ll mod=1e9+7 ; 
int n,q,p; 
int v[N],w[N];
int s[N];
int a[N],cnt=0;unmii mp;
ll pw[N],f[M][M],g[M];
int gcd(int x,int y){
    if (!y)return x;
    return gcd(y,x%y);
}
int main(){
    freopen ("knapsack.in","r",stdin);
    freopen ("knapsack.out","w",stdout);
	scanf("%d%d%d",&n,&q,&p);pw[0]=1;
	for (int i=1;i<=n;i++)pw[i]=pw[i-1]*2%mod;
	for (ll i=1;i*i<=p;i++){
	    if (p%i)continue;
	    a[++cnt]=i;
	    if (i>1&&1ll*i*i!=p)a[++cnt]=p/i;
    }
    sort(a+1,a+cnt+1);
    for (int i=1;i<=cnt;i++)mp[a[i]]=i;
	for (int i=1;i<=n;i++){
	    scanf("%d",&v[i]);s[mp[gcd(v[i],p)]]++;
    }
    for (int i=1;i<=cnt;i++){
        for (int j=1;j<=cnt;j++)f[i][j]=f[i-1][j];
        if (!s[i])continue;
        for (int k=1;k<=cnt;k++){
            if (!f[i-1][k])continue;
            int j=mp[gcd(a[i],a[k])];
            f[i][j]=(f[i][j]+f[i-1][k]*(pw[s[i]]+mod-1)%mod)%mod;
        }
        f[i][i]=(f[i][i]+pw[s[i]]+mod-1)%mod;
    }
    for (int i=1;i<=cnt;i++){
        for (int j=1;j<=cnt;j++){
            if (a[i]%a[j])continue;
            g[i]=(g[i]+f[cnt][j])%mod;
        }
    }
    for (int i=1;i<=q;i++){
        int w;scanf("%d",&w);
        printf("%lld\n",g[mp[gcd(w,p)]]);
    }
    return 0;
}