답안 #1112473

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1112473 2024-11-14T08:37:13 Z chaoslong Sirni (COCI17_sirni) C++14
126 / 140
1280 ms 786432 KB
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"

using namespace std;
const int N = 1e7 + 500;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;

int n, par[N], sz[N];
vector<pii> f[N];

int find(int v) {
    if(v == par[v]) return v;
    return par[v] = find(par[v]);
}

bool join(int x, int y) {
    int sth1 = x, sth2 = y;
    x = find(x); y = find(y);
    if(x != y) {
//        cout << sth1 << " " << sth2 << endl;
//        cout << x << " " << y << endl;
        if(sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        return true;
    }
    return false;
}

void to_nho_cau() {
    cin >> n;
    vector<int> a;
    forr(i, 1, n) {
        int x; cin >> x;
        a.pb(x);
    }
    sort(all(a)); a.resize(unique(all(a)) - a.begin());
    int mx = a.back();
    vector<int> nxt(mx + 500, -1);
    forf(i, 0, a.size()) {
        nxt[a[i]] = i;
    }
    ford(i, mx-1, 0) {
        if(nxt[i] == -1) nxt[i] = nxt[i+1];
    }
    forf(i, 0, a.size()) {
        if(i+1 < a.size())
        f[a[i+1] % a[i]].pb({i, i+1});
//        cout << i << " " << i + 1 << " " << a[i+1] % a[i] << "\n";
        for(int cur = 2 * a[i]; cur <= mx; cur += a[i]) {
            int good = nxt[cur];
            f[a[good] % a[i]].pb({i, good});
//            cout << i << " " << good << " " << a[good] % a[i] << "\n";
        }
    }
    memset(par, -1, sizeof par);
    forr(i, 0, mx) {
        par[i] = i;
        sz[i] = 1;
    }
    ll ans = 0;
    forr(i, 0, mx) {
        for(pii e: f[i]) {
            int u = e.st, v = e.nd;
            if(join(u, v)) {
                ans = ans + i;
            }
        }
    }
    cout << ans << "\n";
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    #ifdef LOCAL
        freopen(file".inp","r",stdin);
        freopen(file".out","w",stdout);
    #endif
    int t = 1;
    //cin >> t;
    while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/
/**
  ∧__∧
(`•ω• )づ__∧
(つ  /( •ω•。)
  しーJ (nnノ) pat pat
**/
/**  /\_/\
*   (= ._.)
*   / >☕ \>💻
**/

Compilation message

sirni.cpp:117:9: warning: "/*" within comment [-Wcomment]
  117 | /**  /\_/\
      |          
sirni.cpp: In function 'bool join(int, int)':
sirni.cpp:37:9: warning: unused variable 'sth1' [-Wunused-variable]
   37 |     int sth1 = x, sth2 = y;
      |         ^~~~
sirni.cpp:37:19: warning: unused variable 'sth2' [-Wunused-variable]
   37 |     int sth1 = x, sth2 = y;
      |                   ^~~~
sirni.cpp: In function 'void to_nho_cau()':
sirni.cpp:6:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
      |                                              ^
sirni.cpp:60:5: note: in expansion of macro 'forf'
   60 |     forf(i, 0, a.size()) {
      |     ^~~~
sirni.cpp:6:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
      |                                              ^
sirni.cpp:66:5: note: in expansion of macro 'forf'
   66 |     forf(i, 0, a.size()) {
      |     ^~~~
sirni.cpp:67:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         if(i+1 < a.size())
      |            ~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 352572 KB Output is correct
2 Correct 316 ms 381516 KB Output is correct
3 Correct 273 ms 352796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 173 ms 274464 KB Output is correct
2 Correct 1151 ms 747028 KB Output is correct
3 Correct 271 ms 353440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 228 ms 352580 KB Output is correct
2 Correct 248 ms 352376 KB Output is correct
3 Correct 246 ms 352700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 211 ms 292208 KB Output is correct
2 Correct 280 ms 320540 KB Output is correct
3 Correct 262 ms 303012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 284236 KB Output is correct
2 Correct 250 ms 305352 KB Output is correct
3 Correct 204 ms 288944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 303504 KB Output is correct
2 Correct 297 ms 337724 KB Output is correct
3 Correct 232 ms 301732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 182 ms 279600 KB Output is correct
2 Correct 309 ms 339124 KB Output is correct
3 Correct 223 ms 303332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 290 ms 366280 KB Output is correct
2 Correct 985 ms 711224 KB Output is correct
3 Correct 277 ms 370120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 311 ms 370880 KB Output is correct
2 Runtime error 1280 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 354900 KB Output is correct
2 Correct 1222 ms 712352 KB Output is correct
3 Correct 220 ms 305072 KB Output is correct