补题链接:Here
A - Number of Multiples
水题
B - An Odd Problem
水题
C - XYZ Triplets
水题,注意数组不要开小了
D - Anything Goes to Zero
这道题思路很妙:
首先计算出字符串中所有 的数量
,然后分三种情况:
此时我们不难发现对每一位的变化,模数要么为
,要么为
,那么我们就可以先按原字符串把两种情况先算出,在计算每一位时进行加减即可,对
位,只需要加上
再对
再对
位,只需要减去
(注意负数取模)再对
(注意负数取模)再对
为下标。
此时不难发现就一个
,那么模数就只能为
,也即
位的变化和计算和上面一样,但对
位的变化和计算和上面一样,但对
,直接输出
即可
最简单的一种情况,全部输出
即可
#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;}}} elsefor (int i = 0; i < n; i++) cout << 1 << endl;return 0;}
