HeHe

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1463    Accepted Submission(s): 475

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<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

题意:

定义He[N]He[N]在[0,N−1][0,N−1]范围内有多少个数满足式子x2≡x (mod N)x2≡x (mod N)

求HeHe[N]=He[1]×……×He[N],He[n]是满足方程解的个数

由欧拉定理

这里φ(n)=2,即小于等于n的素数都满足φ(n)=2    (φ(n)是小于等于n且与n互质的数的个数)每一个素数对应两个满足方程的解

所有He[n]=满足方程解的个数=2num(num是小于n的所有质数的个数)

因为题目让求HeHe函数

HeHe函数是He函数的阶乘

故根据我们上面证明的结论

我们要求He[1],He[2],⋯He[N]He[1],He[2],⋯He[N]

这就用到了阶乘分解因子的方法了,我们知道要求N!中某个因子p有多少个,是不断加N/p直到0位置,而我们需要的只是1-N这些数中有多少个含有p因子,所以加一次N/p即可,然后枚举素因子p即可

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1e7+;
const int N=7e5+;
bool isprime[maxn];
ll prime[N],cnt; void init()//求小于n的所有素数
{
cnt = ;
memset(isprime,true,sizeof(isprime));
for(int i = ; i < maxn; i++)
{
if(isprime[i])
{
prime[cnt++] = i;
for(int j = i + i; j < maxn; j += i)
{
isprime[j] = false;
}
}
}
}
ll q_pow(ll a,ll b,ll mod)
{
ll ans = ;
while(b)
{
if(b & )
ans = ans * a % mod;
b >>= ;
a = a * a % mod;
}
return ans;
} int main()
{
init();
ll n,m,t;
scanf("%lld",&t);
while(t--)
{
ll num=;
scanf("%lld %lld",&n,&m);
for(int i=;prime[i]<=n&&i<cnt;i++)//i<cnt是为了防止数组越界,累加求1->n个数字中,所有素数的个数(包括重复)
{
num=num+n/prime[i];
}
printf("%lld\n",q_pow(,num,m)); }
return ;
}

HDU 2879 数论的更多相关文章

  1. 积性函数,线性筛入门 HDU - 2879

    HDU - 2879HeHe 题意:He[N]为[0,N−1]范围内有多少个数满足式子x2≡x (mod N),求HeHe[N]=He[1]×……×He[N] 我是通过打表发现的he[x]=2k,k为 ...

  2. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  3. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  4. hdu 4542 数论 + 约数个数相关 腾讯编程马拉松复赛

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4542 小明系列故事--未知剩余系 Time Limit: 500/200 MS (Java/Others) ...

  5. hdu 4961 数论?

    http://acm.hdu.edu.cn/showproblem.php?pid=4961 给定ai数组; 构造bi, k=max(j | 0<j<i,a j%ai=0), bi=ak; ...

  6. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

  8. hdu 4059 数论+高次方求和+容斥原理

    http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...

  9. HDU 4651 数论 partition 求自然数的拆分数

    别人的解题报告: http://blog.csdn.net/zstu_zlj/article/details/9796087 我的代码: #include <cstdio> #define ...

随机推荐

  1. Python3 使用requests库读取本地保存的cookie文件实现免登录访问

    1.  读取selenium模块保存的本地cookie文件来访问知乎 读取http://www.cnblogs.com/strivepy/p/9233389.html保存的本地cookie来访问知乎的 ...

  2. 符合mvc思维的分页思想

    .Model Student.cs namespace WebApplication14.Models { public class Student { public int Id { get; se ...

  3. using JSTL

    http://docs.oracle.com/javaee/5/tutorial/doc/bnake.html JSTL(JSP Standard Tag Library)

  4. linux新的API signalfd、timerfd、eventfd使用说明

    原文:http://www.cfanz.cn/?c=article&a=read&id=46555注意很多当前(2013/8/6)线上运营的Linux内核可能不支持! 三种新的fd加入 ...

  5. Backup--查看备份的历史记录

    --==================================================== --查看数据库备份的详细信息:时间存储地址最大/最小LSN SELECT * FROM m ...

  6. 异常:已捕获: "Error creating context 'spring.root': 未将对象引用设置到对象的实例。" (System.Configuration.ConfigurationErrorsException) 捕获到一个 System.Configuration.ConfigurationErrorsException: "Error creating context 'sp

    查看所指定name的context是否注册成功,以后用此容器来获取其中的object. 常见的使用方式: Application_Start中使用ContextRegistry.GetContext( ...

  7. 将以太坊封装为 ERC20

    将以太坊封装为 ERC20 TOKEN 很多 DAPP 都是在处理 ERC20接口的 token, 其实很容易将以太坊封装为 ERC20,这样就可以统一处理, 至少我目前在做的雷电网络就是这么处理的. ...

  8. BeanShell Processor_使用Java处理脚本

    版权声明:本文为博主原创文章,未经博主允许不得转载. [try-catch] 建议使用Try----Catch块,这样Java语句出现问题时,日志更清晰: try { //java代码 } catch ...

  9. [AGC005C]Tree Restoring 构造

    Description ​ 给出一个数组a,要求构造一颗树,使节点x距离最远的点的距离为\(a_x\). Input ​ 第一行一个正整数NN(2≤N≤1002≤N≤100) ​ 接下来一行,有NN个 ...

  10. location的属性

    http://localhost:8080/?a=b#/login location.host "localhost:8080" location.port 8080 locati ...