CodeForces 151C Win or Freeze(简单题)

题目链接

C. 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 cin, cout streams or the %I64d specificator.

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 test(s)
Input
6
Output
2
Input
30
Output
1
6
Input
1
Output
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 30; 6, as we know, is a losing number. Thus, this move will bring us the victory.

题意:桌子上有一个数,两个人轮流操作。每次操作可以把这个变为改数的因子。问先手赢还是输,如果先手赢输出第一手操作。

题解:如果桌子上的数的质因子个数为2则先手输,否则先手赢。(特判1),用sqrt(n)的方法可做。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string.h>
#include<string>
#include<stdlib.h>
typedef __int64 LL;
typedef unsigned __int64 LLU;
const int nn=11000000;
const int inf=0x3fffffff;
const LL inf64=(LL)inf*inf;
using namespace std;
LL x;
LL solve()
{
    LL i;
    LL p=x;
    int num=0;
    LL tem=1;
    for(i=2;i*i<=x;i++)
    {
        while(p%i==0)
        {
            p/=i;
            num++;
            if(num<=2)
                tem*=i;
            if(num>2)
                return tem;
        }
    }
    if(p>1)
    {
        num++;
    }
    if(num==2)
        return -1;
    return num==1?0:tem;

}
int main()
{
    while(scanf("%I64d",&x)!=EOF)
    {
        if(x==1)
        {
            puts("1");
            puts("0");
            continue;
        }
        LL ix=solve();
        if(ix==-1)
        {
            puts("2");
        }
        else
        {
            puts("1");
            printf("%I64d\n",ix);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值