Problem Solving report codeforces150a Win or Freeze

Source: Internet
Author: User
Tags gcd

Problem Solving report codeforces150a Win or Freeze


Description

Can ' t possibly imagine how cold our friends is this winter in nvodsk! The them play the following game to warm up:initially a piece of paper have an integer Q. During a move a player should write any integer number which is a non-trivial divisor of the last written number. Then he should run this number of circles around the hotel. Let us remind-a number ' s divisor is called non-trivial if it's different from one and from the divided Number itself.

The first person who can do 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. ≤?10).

%lld specificator to read or write 64-bit integers inс++. It is preferred to use the cin, cout streams or the %i64dspecificator.

Output

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 E Ven make the first move, print 0). If There is multiple solutions, print any of them.

Sample Input

Input
6
Output
2
Input
30
Output
16
Input
1
Output
10


The main topic: (Spit Groove: Recently do not know why the feeling is not quite understand the problem.) Very obvious game theory, simple Nim game. There is a number where two people take turns taking one of its factors (not necessarily), not 1 or the number itself, and then dividing the number by this factor, first to make the number 1 win. Now give you a number, ask who you are the winner, if it is the first person to win, please output the number of the remaining digits to the opponent after the first hand is taken (any set of solutions)?


Analysis: The problem first decomposition, see how many prime number factor (repeat also to count), if the quality factor (not including the number itself) number >=3, then is 1 victory, because he arbitrarily to opponent 2 left two quality factor product, then 11 set victory. Or this number itself is a prime, then also 1 direct victories. At first did not read the question, finally made clear: first judge is not prime, is the words output 1, 0; if not, then the number of prime table (prime Sieve), and then decomposition of large numbers, find two quality factors on the exit, to see whether the remainder is 1 (1 means only two factors, at this time 2 victory), or 1 victory Leave the product of the two primes to the opponent.


On the code:

#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%prim E[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) | | n Um = = 1) {cout << 1 << endl << 0 << endl;continue;}  for (int i = 0; i<cnt; i++) {if (num%prime[i] = = 0) {if (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;} Elsecout <&Lt 2 << Endl;} return 0;}




But there is a random very hanging judgment primes and large number decomposition method, will knock the template but still do not know the principle.

So let's put it here. Learn the principle tomorrow.


#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstrin G> #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 A, long long b, 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;re Turn;} 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>::it Erator 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;} Elsecout << 2 << Endl;} return 0;}

Feel very hanging.



Problem Solving report codeforces150a Win or Freeze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.