Vae_1118的行列式
Problem:C
Time Limit:1000ms
Memory Limit:65535K
Description
Vae_1118的学妹最近正在学线性代数, 向上学期线性代数低分飘过的Vae_1118求助. 对于一个形如D_1的行列式, 如何快速计算其值? 他很快看出了答案: 对其施以初等列变换, 得到D_2. 因为有两列完全相同, 故有D_1=D_2=0.
现在他的学妹想难为一下他. 考虑将D_1无限延伸, 成为每行每列各成一个等差数列的行列式D_3, 给定T个数a_1,a_2,a_3,…a_t, 求在行列式D_3中, a_1,a_2,a_3,…a_t 分别出现了几次. 由于结果可能很大, 需要对素数P取模.
Vae_1118很快发现这道行列式的题实际上跟行列式的计算没多少关系, 那你能帮帮他吗?
Input
输入为单组数据.
第1行包含两个整数T,P. 以下T行, 每行包含一个整数,分别表示a_1,a_2,a_3,…a_t.
其中T<=50; a<=2^(31)-1; P是质数.
Output
输出包含T行, 每行包含1个整数, 分别表示a_1,a_2,a_3,…a_t 在行列式D_3中出现的次数对质数P取模的结果.
Sample Input
4 10007
2
5
7
82
Sample Output
1
3
4
5
//By Vae_1118
#include <cstdio>
#include <iostream>
#include <cstring>
#define MAXN 500010
using namespace std;
bool isp[MAXN];
int pri[MAXN],cnt;
int e[MAXN];
int t,n,p;
void GetPrime()
{
memset(isp,1,sizeof(isp));
isp[1]=0;
for (int i=2; i<=MAXN; i++) {
if (isp[i]) pri[++cnt]=i;
for (int j=1; j<=cnt && i*pri[j]<=MAXN; j++) {
isp[i*pri[j]]=0;
if (i%pri[j]==0) break;
}
}
}
int main ()
{
ios::sync_with_stdio(0);
GetPrime();
cin>>t>>p;
while (t--)
{
memset(e,0,sizeof(e));
int ans=1;
cin>>n; n--;
for (int i=1;i<=cnt;i++)
{
if (n==1) break;
while (n%pri[i]==0 && n!=1)
{
n/=pri[i];
e[i]++;
}
}
if (n>1) ans=2;//表示n中含有1个且仅含1个大于sqrt(n)的因子.
for (int i=1;i<=cnt;i++)
{
if (e[i])
ans=(ans*(e[i]+1))%p;
}
cout<<ans<<endl;
}
return 0;
}