zoukankan      html  css  js  c++  java
  • AIsing Programming Contest 2020 游记 (ABC水题,D思维)

    补题链接:Here

    A - Number of Multiples

    水题

    B - An Odd Problem

    水题

    C - XYZ Triplets

    水题,注意数组不要开小了

    D - Anything Goes to Zero

    这道题思路很妙:

    首先计算出字符串中所有 (1) 的数量 (cnt) ,然后分三种情况:

    1. (cnt > 1) 此时我们不难发现对每一位的变化,模数要么为 (cnt - 1),要么为 (cnt + 1) ,那么我们就可以先按原字符串把两种情况先算出,在计算每一位时进行加减即可,对 (0) 位,只需要加上 (2^k) 再对 (cnt + 1) 再对 (1) 位,只需要减去 (2^k) (注意负数取模)再对 (cnt + 1) (注意负数取模)再对 (k) 为下标。
    2. (cnt = 1) 此时不难发现就一个 (1) ,那么模数就只能为 (cnt + 1) ,也即 (0) 位的变化和计算和上面一样,但对 (1) 位的变化和计算和上面一样,但对 (0) ,直接输出 (0) 即可
    3. (cnt = 0) 最简单的一种情况,全部输出 (1) 即可
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll power(ll a, ll b, ll mod) { return b ? power(a * a % mod, b / 2, mod) * (b % 2 ? a : 1) % mod : 1; }
    ll cal(ll n) {
        ll cnt = 1;
        while (n) {
            n = n % __builtin_popcount(n);
            cnt++;
        }
        return cnt;
    }
    int main() {
        ll n, cnt = 0, ans1 = 0, ans2 = 0, ans;
        string s;
        cin >> n >> s;
        for (int i = 0; i < n; i++) {
            if (s[i] == '1') cnt++;
        }
        if (cnt > 1) {
            for (ll i = 0; i < n; i++) {
                if (s[i] == '1') ans1 = (ans1 + power(2, n - i - 1, cnt - 1)) % (cnt - 1), ans2 = (ans2 + power(2, n - i - 1, cnt + 1)) % (cnt + 1);
            }
            for (ll i = 0; i < n; i++) {
                if (s[i] == '0') {
                    ans = (ans2 + power(2, n - i - 1, cnt + 1)) % (cnt + 1);
                    cout << cal(ans) << endl;
                } else {
                    ans = (ans1 + ((cnt - 1) - power(2, n - i - 1, cnt - 1) % (cnt - 1)) % (cnt - 1)) % (cnt - 1);
                    cout << cal(ans) << endl;
                }
            }
        } else if (cnt == 1) {
            for (int i = 0; i < n; i++)
                if (s[i] == '1') ans2 = (ans2 + power(2, n - i - 1, cnt + 1)) % (cnt + 1);
    
            for (ll i = 0; i < n; i++) {
                if (s[i] == '0') {
                    ans = (ans2 + power(2, n - i - 1, cnt + 1)) % (cnt + 1);
                    cout << cal(ans) << endl;
                } else {
                    cout << 0 << endl;
                }
            }
        } else
            for (int i = 0; i < n; i++) cout << 1 << endl;
        return 0;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    January 25th, 2018 Week 04th Thursday
    January 24th, 2018 Week 04th Wednesday
    January 23rd, 2018 Week 04th Tuesday
    January 22nd, 2018 Week 04th Monday
    January 21st, 2018 Week 3rd Sunday
    January 20th, 2018 Week 3rd Saturday
    January 19th, 2018 Week 3rd Friday
    January 18th, 2018 Week 03rd Thursday
    January 17th, 2018 Week 03rd Wednesday
    January 16th, 2018 Week 03rd Tuesday
  • 原文地址:https://www.cnblogs.com/RioTian/p/14715905.html
Copyright © 2011-2022 走看看