• Codeforces Round #651 (Div. 2) C. Number Game(数论)


    题目链接:https://codeforces.com/contest/1370/problem/C

    题意

    给出一个正整数 $n$,Ashishgup 和 FastestFinger 依次选择执行以下一个操作:

    • 如果 $n > 1$,使 $n$ 除以一个奇因子
    • 如果 $n > 1$,使 $n$ 减一

    若一方不能操作,则另一方胜利。

    题解

    奇数根据 $n = 1$ 分为两种情况。

    偶数根据是否含有奇因子分为两种情况,不含奇因子根据是否为 $2$ 分为两种情况,含有奇因子根据 $2$ 的个数和奇因子的个数分为四种情况。

    代码一

    #include <bits/stdc++.h>
    using namespace std;
    
    bool isprime(int n) {
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0) return false;
        return true;
    }
    
    void solve() {
        int n; cin >> n;
        char ans = 'X';
        if (n & 1)
            ans = (n == 1 ? 'F' : 'A');
        else
            if ((n & (n - 1)) == 0)
                ans = (n == 2 ? 'A' : 'F');
            else
                ans = (isprime(n / 2) ? 'F' : 'A');
        cout << (ans == 'A' ? "Ashishgup" : "FastestFinger") << "
    ";
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }

    代码二

    #include <bits/stdc++.h>
    using namespace std;
    
    void solve() {
        int n; cin >> n;
        if (n & 1) {
            cout << (n == 1 ? "FastestFinger" : "Ashishgup") << "
    ";
        } else {
            int odd_div = INT_MAX;
            for (int i = 2; i * i <= n; i++) {
                if (n % i == 0) {
                    if (i & 1) {
                        odd_div = min(odd_div, i);
                    } else {
                        int j = n / i;
                        if (j & 1)
                            odd_div = min(odd_div, j);
                    }
                }
            }
            cout << ((odd_div != INT_MAX and n / odd_div != 2) or n == 2 ? "Ashishgup" : "FastestFinger") << "
    ";
        }
    }
    
    int main() {
        int t; cin >> t;
        while (t--) solve();
    }
  • 相关阅读:
    time fly
    小论文初稿终于完成
    leetcode之Length of Last Word
    static关键字
    参数传递
    this关键字
    面向对象有三大特征
    空指针异常
    变量按数据类型分为
    构造方法
  • 原文地址:https://www.cnblogs.com/Kanoon/p/13173450.html
Copyright © 2020-2023  润新知