牛客算法周周练6 game

链接:https://ac.nowcoder.com/acm/contest/5634/C
Nancy喜欢博弈!
Johnson和Nancy得到了一个神奇的多重集合,仅包含一个正整数n,两个人轮流进行操作。
一次操作可以将集合中一个数字分解为它的任意两个非1的因数,并加入集合中。
他们想知道,在Johnson和Nancy绝顶聪明的情况下,如果Nancy先手进行操作,最后谁没有办法继续操作了呢?
输入描述:
第一行:一个整数n。
数据满足:1 \leq n \leq 957181≤n≤95718。
输出描述:
共一行:一个字符串,表示最后谁(Johnson或者Nancy)无法进行操作。
示例1
输入
复制
4
输出
复制
Johnson

每次操作可以将集合中的一个数字分解为它的任意两个非1的因数, 集合中的数字个数+1。
因为 质因数 是无法再被分解的,所以最后集合中的数全为 n 的质因数。因此只需要看题目给定的 n 有多少个质因数。假设 n 有 p 个质因数,那么这场游戏将进行 p-1 次操作(每次操作后集合中的数字个数+1),如果 p-1 为奇数那么后手便无法再进行操作,如果 p-1 为偶数则先手再无法进行操作。

注意:n==1 的情况要特殊处理一下.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
 
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define debug(a) cout << (a) << endl
typedef long long ll;
int dir8[8][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
int dir4[4][2] = { 1, 0, 0, 1, -1, 0, 0, -1 };
const int INF = 0x3f3f3f3fLL;
const long long LLF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXn = 95718 + 15;
const int mod = 20010905;
 
int s[MAXn];
int main()
{
    int n;
    int cnt=0;
    cin>>n;
    if(n==1){ //特殊处理 n==1
        printf("Nancy\n");
        return 0;
    }
    for(int i=2;i<=n;i++){ //求质因数个数
        while(n%i==0)
        {
            n/=i;
            cnt++;
        }
    }
    if(cnt%2==0) //判奇偶
        printf("Johnson\n");
    else printf("Nancy\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值