HeHe
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1328 Accepted Submission(s): 448
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.
Input
First line: an integer t, representing t test cases.
Each test case contains two numbers N (1<=N<=10^7) and M (0
Output
For each test case, output one line, including one integer: HeHe[N] mod m.
Sample Input
1
2 3
Sample Output
2
Source
2009 Multi-University Training Contest 8 - Host by BJNU
Recommend
gaojie
题意: 定义He[x] 为当 x∈[0,N−1] 满足 X2≡X(modN) 求He的前n项积,模上m
分析: He[p^n]=2,其中p是素数.那么He[n]就是2^k,k是不同素数的个数,在1e7的范围中我们可以打表素数,He数组可知,例如He[6] = 2*2,因为6可以分解为2*3,具体证明参考这里,我们预处理出来1e7以内的素数的个数,我们可以一个数一个数的求出,但是太慢了,另一种思路就是,例如当前考虑素数2,我们可以直接一次行筛掉范围内的所有2的倍数的个数,也就是n/2,这样我们依次筛选出来即可,最后快速幂
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e7 + 10;
bool isp[N];
int p[N>>1],tot;
void init() {
for(int i = 2;i <= 1e7;i++) {
if(!isp[i]) p[++tot] = i;
for(int j = 1;j <= tot && i*p[j] <= 1e7;j++) {
isp[i*p[j]] = true;
if(i%p[j] == 0) break;
}
}
}
ll quick_pow(ll b,ll mod) {
ll a = 2;
ll t = 1;
while (b) {
if(b&1) t = t*a%mod;
b >>= 1;
a = a*a%mod;
}
return t;
}
int main(){
ios_base::sync_with_stdio(0);
int T;cin>>T;
init();
while (T--) {
ll n,m;cin>>n>>m;
ll cnt = 0;
for(int i = 1;i <= tot;i++) {
cnt += n/p[i];
if(p[i] > n) break;
}
cout<<quick_pow(cnt,m)<<endl;
}
return 0;
}
- 如有错误或遗漏,请私聊下UP,thx