2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder425の感想と自分が解いたところまでの解説を書いていきます
(今回はAをpythonで書きます)
(AtCoder Beginner Contest 425)
AtCoder Beginner Contest 425

1.感想

A問題:
1回でACできて1分以内に提出できた
B問題:
Nが10までで良かった
C問題:
色々情報量が多かった

2.結果

image.png

3.解説

A問題 Sigma Cubes

C++での解法

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N, ans = 0;
    cin >> N;
    
    for (int i = 1; i <= N; i++){
        ans += floor(pow(-1, i))*i*i*i;
    }
    
    cout << ans << endl;
    return 0;
}

pythonでの解法

N, ans = int(input()), 0

for i in range(1, N+1):
    ans += pow(-1, i)*i*i*i

print(ans)

B問題 Find Permutation 2

C++での解法

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> A(N), P(N, -1), check;
    set<int> used;

    for (int i = 0; i < N; i++) {
        cin >> A[i];
        if (A[i] != -1) {
            P[i] = A[i];
            used.insert(A[i]);
            check.push_back(A[i]);
        }
    }

    sort(check.begin(), check.end());
    for (int i = 1; i < (int)check.size(); i++){
        if (check[i-1] == check[i]){
            puts("No");
            return 0;
        }
    }

    for (int i = 1; i <= N; i++) {
        if (used.find(i) == used.end()) {
            for (int j = 0; j < N; j++) {
                if (P[j] == -1) {
                    P[j] = i;
                    break;
                }
            }
        }
    }

    puts("Yes");
    for (int i = 0; i < N; i++) cout << P[i] << " ";
    cout << endl;

    return 0;
}

C問題 Rotate and Sum Query

C++での解法

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll N, Q, offset = 0;
    cin >> N >> Q;
    vector<ll> A(N);
    for (ll &i : A) cin >> i;
    vector<ll> sum(N + 1, 0);
    for (ll i = 0; i < N; i++) sum[i+1] = sum[i]+A[i];

    for (ll _ = 0; _ < Q; _++) {
        ll type;
        cin >> type;
        if (type == 1) {
            ll c;
            cin >> c;
            offset = (offset+c)%N;
        } else {
            ll l, r, res = 0;
            cin >> l >> r;
            l--, r--;
            ll start = (l+offset)%N, end = (r+offset)%N;
            if (start <= end) res = sum[end+1]-sum[start];
            else res = (sum[N]-sum[start])+sum[end + 1];
            cout << res << endl;
        }
    }

    return 0;
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?