HDU 2879 HeHe(经典积性函数)
来源:互联网 发布:淘宝网 文胸故事模特 编辑:程序博客网 时间:2024/04/29 22:52
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2879
积性函数解释:http://blog.csdn.net/yjx_xx/article/details/25558119
HeHe
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 960 Accepted Submission(s): 269
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
12 3
Sample Output
2
1.证明p是素数时He[p]=2.
x^2=x(mod p)—->p|x(x-1).因为x<p所以p不整除x也不整除x-1.所以成立的情况下是x=1或者x=0.
He[p^k]=2,证明类似上面的
2.证明对于不同的两个素数p和q,He[p*q]=4=He[p]*He[q];
首先x=0和x=1是肯定成立的,
现在由x^2=x(mod p*q)
—>p*q|x(x-1)
假设x=k*p[k<q]
——>p*q|k*p(k*p-1)
——->q|k(k*p-1)
——->q|(k*p-1) 因为k<q q是素数 所以gcd(k,q)=1
——->k*p+t*q=1
这里就变成了这个方程的解,由扩展欧几里得知,这个方程有解,但是k在[0,q-1]之内的解就一个,所以这里多一个解,同理设x=k*p又有一个解,所以x^2=x(mod p*q)有4个解(x=0 ,x=1 ,x=k*p, x=k*q)
—->He[p*q]=4=He[p]*He[q];
那么He[p1^r1*p2^r2*……*pk^rk]=2^k然后可以进一步算出HeHe只需要算n以内每个素数的倍数的个数.AC代码:
#include <stdio.h>#include <string.h>#define LL __int64#define max 10000001int prime[max];void getprime(){LL i,j;memset(prime,0,sizeof(prime));for(i=2;i<max;i++){if(!prime[i]){for(j=i;j<max;j+=i)prime[j]++;//这素数的倍数个数}}for(i=2;i<max;i++)prime[i]+=prime[i-1];//算素数之间的数与这个数之前的素数的个数一样}LL qp(LL n,LL m){LL a=2,ans=1;//算2的总的素数个数的幂while(n){if(n&1)ans*=a;n>>=1;a*=a;a%=m;ans%=m;}return ans%m;}int main(){getprime();int t;LL n,m,i;scanf("%I64d",&t);while(t--){scanf("%I64d%I64d",&n,&m);printf("%I64d\n",qp(prime[n],m));}return 0;}
0 0
- HDU 2879 HeHe(经典积性函数)
- HDU 2879 HeHe 【素数+积性函数】
- hdu 2879 HeHe 数论 积性函数 优化技巧
- hdu 2879 HeHe (数论:积性函数+欧拉定理+快速幂)
- HDU2879 HeHe 数论积性函数
- HeHe HDU
- HDU 4639 Hehe
- HDU 4639 Hehe(DP)
- hdu 4639 Hehe (dp)
- HDU-4639-Hehe
- hdu 4639 Hehe
- HDU - 5084 HeHe
- HDU 4639 Hehe 解题报告
- HDU 5084 HeHe --找规律
- HEHE
- hehe
- hehe
- HEHE
- 用shutdown和close分别关闭套机字的区别
- 一门赚钱的生意方法,是如何变成的
- linux中统计字节数、字数、行数命令wc用法详解
- 基于MVC4+EasyUI的Web开发框架形成之旅--框架总体界面介绍
- asp.net Dataset() 基础学习
- HDU 2879 HeHe(经典积性函数)
- LeetCode_Single Number&Single NumberII
- unix环境编程 目录操作
- JS中的onload与jQuery中的ready区别
- 预处理调试标记
- LeetCode_Candy
- QT中的进程和线程
- SELinux简单学习
- SVM入门(七)为何需要核函数