题意比较简单,首先我们可以确定he(x)是积性函数。证明是比较容易的。由he函数的定义可以得出x*(x-1)=k*n这个式子。我们设x*(x-1)=k1*n1,x'*(x'-1)=k'*n';并设he(x)=a,he(x')=b;则he(x*x')=a*b。因为he(x)是积性函数,所以我们只需要把质数的he值算出来即可。这时,我们再观察,如果一个合数由不同的质数组成,如30=2*3*5,则he(30)=he(2)*he(3)*he(5),由题意知,每个质数的he值都是2,所以he(30)=8。我们再考虑有重复质数的情况,可以由证明是积性函数的方法知,其实有重复的质因数和只有一个质因数是一样的。所以问题就转化为一个数分解后有几个不同的质因数,即有2的多少次方满足条件。程序中再用筛法优化一下就可以了。题目:
HeHe
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 641 Accepted Submission(s): 202
Problem Description
In the equation X^2≡X(mod N) where x∈[0,N-1], we define He[N] as the number of solutions.
And furthermore, define HeHe[N]=He[1]*……*He[N]
Now here is the problem, write a program, output HeHe[N] modulo M for a given pair N, M.
And furthermore, define HeHe[N]=He[1]*……*He[N]
Now here is the problem, write a program, output HeHe[N] modulo M for a given pair N, M.
Input
First line: an integer t, representing t test cases.
Each test case contains two numbers N (1<=N<=10^7) and M (0<M<=10^9) separated by a space.
Each test case contains two numbers N (1<=N<=10^7) and M (0<M<=10^9) separated by a space.
Output
For each test case, output one line, including one integer: HeHe[N] mod m.
Sample Input
1 2 3
Sample Output
2
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string.h>
using namespace std;
#define M 10000005
int num[M];
//int sum[M];
int n,m;
void init(){
memset(num,0,sizeof(num));
//memset(sum,0,sizeof(sum));
num[1]=0;
for(int i=2;i<=M;++i){
if(!num[i]){
for(int j=i;j<=M;j+=i)
num[j]++;
}
}
for(int i=1;i<=M;++i)
num[i]=num[i-1]+num[i];
}
_int64 binary_search(int x){
if(x==1)
return 2;
_int64 ans=binary_search(x/2);
return (ans*ans%m)*((x%2?2:1)%m)%m;
}
int main(){
int numcase;
init();
/* for(int i=1;i<=50;++i)
printf("%d ",num[i]);*/
scanf("%d",&numcase);
while(numcase--){
scanf("%d%d",&n,&m);
int y=num[n];
/*printf("y==%d\n",y);*/
_int64 x=binary_search(y);
printf("%I64d\n",x);
}
return 0;
}