AIsing Programming Contest 2020

A B C D E F

https://atcoder.jp/contests/aising2020

A.

How many multiples of d are there among the integers between L and R (inclusive)?

#include 

using namespace std;
typedef long long LL;
int main(){
	int l, r, d;
	cin>>l>>r>>d;
	int cnt = 0;
	for(int i = l; i <= r; i++) if(i % d == 0) cnt++;
	cout<

B.

输出下标、值均为奇数的数的个数。题目中Square不是平方相关…

#include 

using namespace std;
typedef long long LL;
int main(){
	int n;
	cin>>n;
	int a[200];
	int cnt = 0;
	for(int i = 0; i < n; i++)cin>>a[i];
	for(int i = 0; i < n; i++) if(a[i] & 1 && (i + 1) & 1) cnt++;
	cout<

C

Let f(n)f(n) be the number of triples of integers (x,y,z)(x,y,z) that satisfy both of the following conditions:

  • 1≤x,y,z
  • x 2 + y 2 + z 2 + x y + y z + z x = n x 2 + y 2 + z 2 + x y + y z + z x = n x^2+y^2+z^2+xy+yz+zx=nx^2+y^2+z^2+xy+yz+zx=n x2+y2+z2+xy+yz+zx=nx2+y2+z2+xy+yz+zx=n

Given an integer N, find each of f(1),f(2),f(3),…,f(N). 1≤N≤104
求符合条件的(x,y,z)个数
需要注意到在n的限制下100是x,y,z的上界,直接对xyz遍历。

#include 

using namespace std;
typedef long long LL;
int main(){
    int n;
    cin>>n;
    int ans[20000] = {};
    for(int i = 1; i <= 100; i++){
        for (int j = 1; j <= 100; ++j) {
            for (int k = 1; k <= 100; ++k) {
                if(i * i + j * j + k * k + i * j + i * k + j *k < 10004)
                ans[i * i + j * j + k * k + i * j + i * k + j *k] ++;
            }
        }
    }

    for (int l = 0; l < n; ++l) {
        cout<

D

  • 题目大意:
    p ( x ) : x → x 二 进 制 表 示 下 1 的 个 数 p(x) : x \to x二进制表示下1的个数 p(x):xx1
    Q : x → x m o d    g ( x ) Q: x \to x \mod g(x) Q:xxmodg(x)
    f ( x ) : 使 用 变 换 Q 将 x 变 换 为 0 的 最 小 变 换 次 数 f(x): 使用变换Q将x变换为0的最小变换次数 f(x):使Qx0
    X为N位二进制数, X N = X   x o r ( 1 < < n ) X_N = X\space xor (1 << n) XN=X xor(1<<n)
    求:

f ( X 1 ) , f ( X 2 ) , … , f ( X N ) , 1 ≤ N ≤ 2 × 1 0 5 f(X_1), f(X_2), \ldots, f(X_N) , 1 \leq N \leq 2 \times 10^5 f(X1),f(X2),,f(XN),1N2×105

  • f(x) = f(Q(x)) + 1, x 在int 范围内时可以使用记忆化搜索求得f(x)
  • x在int范围外时,注意到Q(x)在int范围内,用快速幂求出Q(x)即可
  • 对于X1-Xn, 由于只和x差一个二进制位,且g(xi) = g(x) ±1,预处理1-2^N mod g(x) ±1 和 X mod g(x) ±1 的值后,Q(xi)可以在O(1)复杂度内求得。需要注意Q(xi) = 0的情况。
#include 

using namespace std;
typedef long long LL;
int ff[200005] = {};

int f(int x) {
    if (x == 0)return 0;
    if (ff[x]) return ff[x];
    int cnt1 = 0;
    for (int i = 0; i < 20; i++) {
        if (x & (1 << i))cnt1++;
    }
    int i1 = x % cnt1;
    if (i1 == 0) {
        ff[x] = 1;
        return 1;
    }
    return ff[x] = f(i1) + 1;
}
int mmd[200005];
string s;
int ans[200006];
int main() {
    int n;
    cin >> n;
    int p = 0;
    cin >> s;
    int cnt1 = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '1') cnt1++;
    }

    int mmd[3] = {};
    int d = 1;
    if(cnt1 >= 2) {
        for (int i = 0; i < n; ++i) {
            if(s[n - i - 1] == '1'){
                mmd[0] += d;
                mmd[0] %= cnt1 - 1;
            }
            d *= 2;
            d %= cnt1 - 1;
        }
    }

    d = 1;
    if(cnt1 >= 1){
        for (int i = 0; i < n; ++i) {
            if(s[n - i - 1] == '1'){
                mmd[1] += d;
                mmd[1] %= cnt1;
            }
            d *= 2;
            d %= cnt1;
        }
    }

    d = 1;
    for (int i = 0; i < n; ++i) {
        if(s[n - i - 1] == '1'){
            mmd[2] += d;
            mmd[2] %= cnt1 + 1;
        }
        d *= 2;
        d %= cnt1 + 1;
    }

    int d1 = 1;
    int d2 = 1;
    for (int i = 0; i < n; ++i) {
        bool add1 = s[n - i - 1] == '0';
        int md = cnt1 + (add1 ? 1 : -1);
        if(md)ans[n - i - 1] = f((mmd[add1 ? 2: 0] + (add1 ? d1 : -d2) + md) % md) + 1;
        else ans[n - i - 1] = 0;
        d1 *= 2;
        d2 *= 2;
        d1 %= cnt1 + 1;
        if(cnt1 - 1 != 0) d2 %= cnt1 - 1;
    }


    for (int i = 0; i < n; ++i) {
        cout<

你可能感兴趣的:(acm练习)