Codeforces 150A Win or Freeze (博弈 & 数论)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/i1020/article/details/79975849

A. Win or Freeze
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 cincout 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.

Examples
input
Copy
6
output
Copy
2
input
Copy
30
output
Copy
1
6
input
Copy
1
output
Copy
1
0
Note

Number 6 has only two non-trivial divisors: 2 and 3. It is impossible to make a move after the numbers 2 and 3 are written, so both of them are winning, thus, number 6 is the losing number. A player can make a move and write number 6 after number 306, as we know, is a losing number. Thus, this move will bring us the victory.

【题目链接】点击有惊喜

【题意】给定一个数n,两个人轮流写出当前数的因子,则当前的数n也就变成了该选手枚举的因子。当某一选手面临的数n为1或者质数的时候,该选手获胜。如果先手获胜,输出1,并且输出先手写下的第一个因子(即给定的n的因子),否则输出2。

【思路】假设两个素数pi,pj,如果n==pi*pj(i可能等于j),则后手胜;否则先手胜。

【代码如下】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll n;
vector<ll>vct;

int main(){
    cin >> n;
    ll x = n;
    for(ll i = 2; i*i <= n; i ++){
       while(n%i==0){
            vct.push_back(i); n /= i;
       }
    }
    if(x!=n && n!=1) vct.push_back(n);
    if(vct.size() > 2) cout << "1" << endl << vct[0]*vct[1] << endl;
    else if(vct.size() == 2) cout << "2" << endl;
    else cout << "1" << endl << "0" << endl;
    return 0;
}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页