ACM 数论 hdu 1395 2^x mod n = 1
2016-05-12 13:53
369 查看
[align=left]Problem Description[/align]
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
[align=left]Input[/align]
One positive integer on each line, the value of n.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
这道题数据简单,暴力求解可以出来,但真要用思维做,需要用到欧拉函数,在此先不用了
但是注意,当x为1或者偶数时,是绝对不可能实现的,首先pass
其余的进行暴力求解法即可
#include<stdio.h>
int main(){
int x,n,t;
while(scanf("%d",&n)!=EOF){
if(n==1 || n%2==0)
{
printf("2^? mod %d = 1\n",n);
}
else
{
x=1;
t=2;
while(t%n!=1){
x++;
t=t*2%n; //反之数值过大
}
printf("2^%d mod %d = 1\n",x,n);
}
}
return 0;
}
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
[align=left]Input[/align]
One positive integer on each line, the value of n.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 5
[align=left]Sample Output[/align]
2^? mod 2 = 1 2^4 mod 5 = 1
这道题数据简单,暴力求解可以出来,但真要用思维做,需要用到欧拉函数,在此先不用了
但是注意,当x为1或者偶数时,是绝对不可能实现的,首先pass
其余的进行暴力求解法即可
#include<stdio.h>
int main(){
int x,n,t;
while(scanf("%d",&n)!=EOF){
if(n==1 || n%2==0)
{
printf("2^? mod %d = 1\n",n);
}
else
{
x=1;
t=2;
while(t%n!=1){
x++;
t=t*2%n; //反之数值过大
}
printf("2^%d mod %d = 1\n",x,n);
}
}
return 0;
}
相关文章推荐
- mysql-5.7.12免安装版下载和配置
- 打字第一次到良好
- python set(集合)数据结构
- Android数据存储五种方式总结
- 小儿医学常识与用药篇
- 命令模式
- 计蒜客 20 跳跃游戏二
- 基于BootStrap Metronic开发框架经验小结【九】实现Web页面内容的打印预览和保存操作
- python3在用pip装lxml模块遇到的问题
- 正则
- android 微信登录
- 网络流SAP
- Docker部署Rancher Server-centos7
- iOS 常用第三方开源框架介绍
- Codeforces Round #352 (Div. 2) ABCD
- thrift系列 - harpc 基于thrift的轻量级rpc框架
- SpringMVC @ResponseBody 415错误处理
- HDOJ 1007-Quoit Design【模板:多点求最短距离(非整数点)】
- 语法分析(5)...
- 剑指offer之面试题27二叉搜索树与双向链表