2^x mod n = 1
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
Author
MA, Xiao
Source
题目类型:欧拉定理+暴力枚举
算法分析:直接排除n为1和偶数的情况,此时一定无解。若n为大于1的奇数,则使用欧拉定理得出合理的解m,最后枚举1~tt中所有的指数,对于每个指数使用快速幂判断是否余数为1,更新最小值,当然更好的方法是枚举n的所有因子,直接判断因子是否满足条件。这道题的数据比较水,所以暴力也可过~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-6; const double PI = 2 * acos (0.0); const int maxn = 2; LL phi (LL val) { LL res = val; for (LL i = 2; i * i <= val; i++) { if (val % i == 0) { res -= res / i; while (val % i == 0) { val /= i; } } } if (val > 1) res -= res / val; return res; } LL mod_f (LL a, LL b, LL p) { LL res = 1, tt = (a % p + p) % p; while (b) { if (b & 1) res = res * tt % p; tt = tt * tt % p; b >>= 1; } return res; } int main() { // CPPFF; LL n; while (cin >> n) { if (n == 1 || ((n & 1) == 0)) cout << "2^? mod " << n << " = " << "1" << endl; else { LL tt = phi (n), minval = tt; for (LL i = 1; i <= tt; i++) { if (mod_f (2, i, n) == 1) { minval = i; break; } } cout << "2^" << minval << " mod " << n << " = " << "1" << endl; } } return 0; } |
- « 上一篇:hdu1251
- hdu1421:下一篇 »