HD_1395 2^x mod n = 1
2015-12-10 19:36
246 查看
2^x mod n = 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14852 Accepted Submission(s): 4576
Problem Description
Give a number n, find the minimum x(x>0) that satisfies 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=a%n;这样数据量可以减少,程序运行的时间就会减少。代码如下:
#include<stdio.h> int main(){ int n,k,a; while(scanf("%d",&n) == 1){ if( n%2 == 0 || n==1) printf("2^? mod %d = 1\n",n); else{ k=1,a=2; while(k++){ a*=2; a%=n; if(a==1) break; } printf("2^%d mod %d = 1\n",k,n); } //printf("\n"); } return 0; }
相关文章推荐
- iOS中 简单抽屉效果(MMDrawerController)的实现
- zzulioj--1801--xue姐的小动物(水题)
- servlet打开一个jsp莫名乱码问题
- C++11 学习笔记 模板的细节(右尖括号,using,函数模板默认参数)
- requirejs官网
- zzulioj--1801--xue姐的小动物(水题)
- Error:Execution failed for task ':app:processDebugResources'com.android.ide.common.process.ProcessEx
- [Prufer] BZOJ 1211 [HNOI2004]树的计数
- 不容易系列之(4)——考新郎
- 持续集成
- 第三个Sprint冲刺第六天
- Android之Menu
- cordova ionic相关命令
- 《iOS Human Interface Guidelines》——Case Study:From Desktop to iOS
- UIScrollView的重用笔记
- NSString 转NSURL的时候会出现NSURL为空
- FLV科普3 数据存放方式
- 浅析Java虚拟机结构与机制
- MFC基本教程&小程序
- BZOJ2330(差分约束)