Enjoy3 数列计数
zzr8178541919 · · 个人记录
题目描述
kal0rona 想要得到一个长度为 n 的数列,数列中的数都是不超过 m 的正整数,而且这 n 个数的和是 p 的倍数。
kal0rona 还希望,这 n 个数中,至少有一个数是质数。
kal0rona 想知道,有多少个数列满足他的要求。
输入格式
一行三个数n、m 和 p。
输出格式
一行一个数,满足 kal0rona 要求的数列的数量。
由于满足条件的数列可能很多,输出结果对20170408 取模。
题解
非常诡异的解法,不是很理解
对dp数组进行了改装,使之可以进行快速幂操作,类似于矩阵乘法
先贴code:
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define reg register
using namespace std;
const int maxn=1e6+5;
const int max_N=2e7+5;
const int mod=20170408;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m,p;
int prime[max_N];
int tot=0;
long long g[105];
int check[max_N];
long long f[105];
long long F[105];
long long G[105];
void Pre(int maxx)
{
F[0]=f[1]=G[0]=g[1]=1;
check[1]=1;
for(int i=2;i<=maxx;i++)
{
f[i%p]++;
if(check[i]==0)
prime[++tot]=i;
else
g[i%p]++;
for(int j=1;j<=tot && 1ll*prime[j]*i<=maxx;j++)
{
check[prime[j]*i]=1;
if(i%prime[j]==0)
break;
}
}
}
int c[205];
void mul(long long *a,long long *b)
{
for(int i=0;i<=p-1;i++)
{
for(int j=0;j<=p-1;j++)
{
c[i+j]=(c[i+j]%mod+a[i]*b[j])%mod;
}
}
for(int i=0;i<=p-1;i++)
{
a[i]=(c[i]+c[i+p])%mod;
c[i]=c[i+p]=0;
}
}
int main()
{
n=read(),m=read(),p=read();
Pre(m);
while(n)
{
if(n & 1)
{
mul(F,f);
mul(G,g);
}
mul(f,f);
mul(g,g);
n>>=1;
}
printf("%lld\n",((F[0]-G[0])%mod+mod)%mod);
return 0;
}
/*