hdu1395 (数论,暴力求余)
来源:互联网 发布:淘宝直通车省钱助手 编辑:程序博客网 时间:2024/05/16 10:38
题目大意:给你一个数N,判断是否存在x,满足2^x mod N = 1。若
满足,对于满足条件的最小x,输出2^x mod N = 1,否则输出
Input
One positive integer on each line, the value of n.
Output
If the minimum x exists, print a line with 2^x mod n = 1.
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
Sample Input
2
5
Sample Output
2^? mod 2 = 1
2^4 mod 5 = 1
思路(转):思路:用到数论上的乘法逆元的规律了。
乘法逆元:对于整数a、p如果存在整数b,满足a*b mod p = 1,则称
b是a的模p的乘法逆元。a存在模p的乘法逆元的充要条件是gcd(a,p) = 1
此题中,令a = 2^x,b = 1,p = n,则若存在x使得2^x mod N = 1,
则gcd(2^x,N) = 1。
1>.因为N>0,当N为偶数时,gcd(2^x,N) = 2*k(k=1,2,3……),不满足
2>.当N为奇数时,gcd(2^x,N) = 1满足条件。
3>.当N为1时,2^x mod N = 0,不符合条件
所以N为奇数,且不为1,满足2^x mod N = 1,暴力。
代码:
#include<stdio.h>int main(){ int n; while(~scanf("%d",&n)) { if(n==1||!(n&1)) //n&1进行与运算,就是n必须为奇数; { printf("2^? mod %d = 1\n",n); continue; } int ans=2,num=1; while(ans!=1) { ans=ans*2%n; num++; } printf("2^%d mod %d = 1\n",num,n); }}
0 0
- hdu1395 (数论,暴力求余)
- HIToj How many N 1008 (数论同余&暴力)
- HDU1395 ZOJ1489 2^x mod n = 1【暴力法+数论】
- HDU2303(数论)大整数求余+素数筛选
- 数论(同余+hash)
- 大数求余(String求余)
- The Embarrassed Cryptographer POJ 2635 数论之高精度求模(同余模定理+千进制)
- POJ 2115 for求循环次数-数论-(同余方程+扩展欧几里得算法)
- hdu1395
- hdu1395
- Poj 1061 青蛙的约会 数论 欧几里得 求余方程
- POJ2769 Reduced ID Numbers 求余应用-暴力
- POJ 2635 The Embarrassed Cryptographer [高精度求余+同余模定理]【数论】
- 《数论》3.6习题3------求一元线性同余方程所有解(不是方程组)基础练习例子&&求逆元
- POJ 3980(求余)
- 求余算法(非%!!)
- 求余运算(分治)
- 负数求余(模)
- POJ3613 Cow Relays
- Java Servlet Development Without Eclipse
- Vulkan Instance
- stm32小小中断学习
- poj 3278 catch the cow
- hdu1395 (数论,暴力求余)
- [BZOJ1061] [NOI2008] 志愿者招募 - 最小费用最大流
- android中可以通过两种方式调用接口发送短信
- 并查集详解
- Guava线程池踩坑记录
- Duplicate files copied in APK META-INF/notice.txt错误的解决办法
- Stanford机器学习-- 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
- CAS原子操作实现无锁及性能分析
- ease.js "Uncaught Ticker cannot be instantiated."