3 分钟阅读

CodeForces1043 div3 题解

比赛地址

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        string a; cin >> a;
        int m; cin >> m;
        string b; cin >> b;
        string order; cin >> order;
        
        int l = 0;
        
        for (char c : order) {
            if (c == 'V') {
                a = b[l++] + a;
            } else {
                a = a + b[l++];
                
            }
        }
        
        cout << a << '\n';
    }
}
#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        i64 n; cin >> n;
        
        vector<i64> a;
		
        i64 cur_10 = 10;
        for (int k = 1; k < 18; k++) {
            i64 num = cur_10 + 1;
            if (n % num == 0) {
                a.push_back(n / num);
            }
            cur_10 *= 10;
        }

        if (a.size() == 0) {
            cout << 0 << '\n';
            continue;
        }

        reverse(a.begin(), a.end());
        
        cout << a.size() << '\n';
        for (i64 n : a) {
            cout << n << ' ';
        }
        
        cout << '\n';
    }
}

B. 秘密数字

V 有一个数字 x ,为了不让人猜到这个数字,他向这个数字的右边添加若干个0

得到一个新的数字y,给定数字n,找到所有符合 n = x + y 的数字x

推导过程:

\[y = 10 ^ k * x\] \[n = x + y = x* (10^k + 1)\] \[x = n / (10^k + 1)\]

n的取值范围为[1, \(10^{18}\)]

则k取 [1, 18]即可

对于每一个n,如果没有合适的x,输出0

其余情况,输出合适x的数量,再按照增序,输出x

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        i64 n; cin >> n;
        vector<i64> a;
        
        i64 tmp = 10;
        for (int i = 1; i < 18; i++) {
            // 每一次循环 tmp 都需要 *10
            i64 num = tmp + 1;
            // 如果取余结果为0,表示可以被整除
            if (n % num == 0) {
                // 每一次将 n / num 放入数组
                a.push_back(n / num);
            }
            
            tmp *= 10;
        }
        
        reverse(a.begin(), a.end());
        int cnt = a.size();
        
        if (cnt == 0) {
            cout << 0 << '\n';
            continue;
        }
        
        cout << cnt << '\n';
        for (i64 num : a) {
            cout << num << ' ';
        }
        
        cout << '\n';
    }
    
    return 0;
}

C.

商人售卖西瓜,售卖\(3^x\)个西瓜可以获得\(3^{x+1} + x*3^{x-1}\)个硬币

可以推出:1个西瓜,获得3个硬币

3个西瓜,获得10个硬币

9个西瓜,获得33个硬币

27个西瓜,获得81+27=108个硬币

20个西瓜,9个 + 9个 + 2个

66+6

n个西瓜,得到

\[x_0*3^0 + x_1 * 3^1 + ... + x_n * 3^{n}\] \[f(x) = 3^{x+1} + x*3^{x-1}\]

其中硬币数为:

\[sum = f(0) * x_1 + f(1) * x_2 + ... + f(n-1)*x_{n}\]

20

20 > 9 ;20 / 9 = 2

ans += 2 * f(2)

3**17 = 1e9+…

以下代码将Gemini和GPT都误导了,导致没有通过样例3

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int getK(i64 n) {
    int k = 0;
    while (n) {
        n /= 3;
        k++;
    }
    return k - 1;
}

i64 pow3(i64 k) {
    i64 res = 1;
    for (int i = 0; i < k; i++) {
        res *= 3;
    }
    
    return res;
}

i64 calCoin(int n) {
    if (n == 0) return 3;
    return pow(3, n+1) + n * pow(3, n-1);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        i64 ans  = 0;
        while (n) {
            for (int i = 17; i >= 0; i--) {
                i64 curN = pow3(i);
                if (n >= curN) {
                    int cnt = n / curN;
                    ans += 1ll * cnt * calCoin(getK(curN));
                    n %= curN;
                }
            }
        }
        
        cout << ans << '\n';
    }
    
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    i64 p[20];
    p[0] = 1;
 	
    for (int i = 1; i < 20; i++) {
        p[i] = p[i-1] * 3;
    }
    
    i64 cost[20];
    const[0] = 3;
    
    for (int k = 1; k < 20; k++) {
        cost[k] = p[k+1] + 1ll * k * p[k-1];
    }
    
    int t; cin >> t;
    while (t--) {
        i64 n; cin >> n;
        i64 ans = 0;
        for (int k = 0; n > 0; k++) {
            int cnt = n % 3;
            ans += 1ll * cnt * cost[k];
            n /= 3;
        }
        
        cout << ans << '\n';
    }
    
    return 0;
}

标签:

分类:

更新时间: