解题报告 之 CodeForces150A Win or Freeze
Description
You can‘t possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer q. During a move a player should
write any integer number that is a non-trivial divisor of the last written number. Then he should run this number of circles around the hotel. Let us remind you that a number‘s divisor is called non-trivial if
it is different from one and from the divided number itself.
The first person who can‘t make a move wins as he continues to lie in his warm bed under three blankets while the other one keeps running. Determine which player wins considering that both players play optimally. If
the first player wins, print any winning first move.
Input
The first line contains the only integer q (1?≤?q?≤?1013).
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecificator.
Output
In the first line print the number of the winning player (1 or 2). If the first player wins then the second line should contain another integer — his
first move (if the first player can‘t even make the first move, print 0). If there are multiple solutions, print any of them.
Sample Input
Input
6
Output
2
Input
30
Output
1 6
Input
1
Output
1 0
题目大意:(吐槽:最近不知道为何感觉都读不太懂题。) 很明显的博弈论,简单的Nim游戏。有一个数字,两个人轮流取它的某因数(不一定),不能是1或者这个数本身,然后将数除以这个因数,先让这个数变为1的人获胜。现在给你一个数字,问你是哪个人获胜,如果是第一个人获胜请输出他第一手取之后给对手剩下的数是多少(任意一组解)?
分析:这个题先把数分解了,看有多少个质数因子(重复的也要算进去),如果质因子(不包含数本身)数量>=3,那么就是1胜利,因为他任意给对手2留下两个质因子的乘积,那么1一定胜利。或者这个数本身是质数,那么也是1直接胜利。一开始没读懂题,最后终于搞清楚了:先判断是不是素数,是的话输出1,0;如果不是,则打素数表(素数筛法),然后分解大数,找到两个质因子就退出,看剩下的数是不是1(是1表示仅有两个因子,此时2胜利),否则1胜利,给对手留下这两个素数的乘积即可。
上代码:
#include<iostream> #include<algorithm> using namespace std; int isprime[4000000]; long long prime[2000000]; int cnt = 0; int fac = 1; void getprime() { for (int i = 2; i < 4000000; i++) isprime[i] = 1; prime[1] = 0; for (int i = 2; i < 4000000; i++) { if (!isprime[i]) continue; for (int j = i * 2; j < 4000000; j += i) isprime[j] = 0; prime[cnt++] = i; } } int isPrime(long long n) { if (n < 4000000) return isprime[n]; for (int i = 0; prime[i] * prime[i] <= n; i++) if (n%prime[i] == 0) return 0; return 1; } int main() { long long num; getprime(); while (cin >> num) { int sum = 0; fac = 1; long long tem = num; if (isPrime(num) || num == 1) { cout << 1 << endl << 0 << endl; continue; } for (int i = 0; i<cnt; i++) { if (num%prime[i] == 0) { while (num%prime[i] == 0) { if (sum <= 2) fac *= prime[i]; sum++; num /= prime[i]; if (sum >= 2) break; } } if (sum >= 2) break; } if (sum >=2&&num!=1) { cout << 1 << endl; cout << fac << endl; } else cout << 2 << endl; } return 0; }
不过有一种随机化很吊的判断素数和大数分解法,会敲模板但是还不知道原理。
于是乎先摆在这吧明天再学习原理。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <map> using namespace std; const int times = 50; int number = 0; int fac = 1; map<long long, int>m; long long Random(long long n) { return ((double)rand() / RAND_MAX*n + 0.5); } long long multi(long long a, long long b, long long mod) { long long ans = 0; while (b) { if (b & 1) { b--; ans = (ans + a) % mod; } else { b /= 2; a = (a + a) % mod; } } return ans; } long long Pow(long long a, long long b, long long mod) { long long ans = 1; while (b) { if (b & 1) { b--; ans = multi(ans, a, mod); } else { b /= 2; a = multi(a, a, mod); } } return ans; } bool witness(long long a, long long n) { long long d = n - 1; while (!(d & 1)) d >>= 1; long long t = Pow(a, d, n); while (d != n - 1 && t != 1 && t != n - 1) { t = multi(t, t, n); d <<= 1; } return t == n - 1 || d & 1; } bool miller_rabin(long long n) { if (n == 2) return true; if (n<2 || !(n & 1)) return false; for (int i = 1; i <= times; i++) { long long a = Random(n - 2) + 1; if (!witness(a, n)) return false; } return true; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a%b); } long long pollard_rho(long long n, long long c) { long long x, y, d, i = 1, k = 2; x = Random(n - 1) + 1; y = x; while (1) { i++; x = (multi(x, x, n) + c) % n; d = gcd(y - x, n); if (1<d&&d<n) return d; if (y == x) return n; if (i == k) { y = x; k <<= 1; } } } void find(long long n, long long c) { if (n == 1) return; if (miller_rabin(n)) { m[n]++; number++; if (number <= 2) fac *= n; return; } long long p = n; while (p >= n) p = pollard_rho(p, c--); find(p, c); find(n / p, c); } int main() { long long n; while (cin>>n) { m.clear( ); number = 0; fac = 1; find(n, 2013729); /* map<long long, int>::iterator it = m.begin(); for (; it != m.end( ); it++) cout << it->first << endl; */ if (n==1||miller_rabin(n)) { cout << 1 << endl << 0 << endl;; continue; } if (number > 2 ) { cout << 1 << endl; cout << fac; cout << endl; } else cout << 2 << endl; } return 0; }
感觉很吊。。