Submission #1060461

# Submission time Handle Problem Language Result Execution time Memory
1060461 2024-08-15T15:24:32 Z khanhtb Sirni (COCI17_sirni) C++14
84 / 140
650 ms 786432 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7; 
const ll inf = 2e18;
const ll B = 320;
const ll N = 1e7+8;
ll n;
vector<ll> a;
vector<pair<ll,ll>> edge_mod[N];
ll par[N];
ll fs(ll v){
    return ((par[v]<0)?v:par[v]=fs(par[v]));
}
bool ss(ll u, ll v){
    return fs(u) == fs(v);
}
void us(ll u, ll v){
    if(ss(u,v)) return;
    ll x = fs(u);
    ll y = fs(v);
    if(par[x] > par[y]) swap(x,y);
    par[y]+=par[x];
    par[x]=y;
}
ll tw = 0;
void solve(){
    cin >> n;
    fill(par,par+n+10,-1);
    for(ll i = 1; i <= n; i++){
        ll x;cin >> x;
        a.pb(x);
    }
    ll ans = 0;
    sort(all(a));
    a.erase(unique(all(a)),a.end());
    ll mx = a.back();
    vector<ll> opt(mx+1,-1);
    for(ll i = 0; i < a.size(); i++) opt[a[i]] = i;
    for(ll i = mx-1; i >= 0; i--){
        if(opt[i] == -1) opt[i] = opt[i+1];
    }
    for(ll i = 0; i < a.size()-1; i++){
        edge_mod[a[i+1] % a[i]].pb({i+1,i});
        for(ll j = 2*a[i]; j <= mx; j += a[i]){
            ll p = opt[j];
            edge_mod[a[p] % a[i]].pb({i,p});
        }
    }
    for(ll md = 0; md <= mx; md++){
        for(pair<ll,ll> &e:edge_mod[md]){
            if(ss(e.fi,e.se)) continue;
            tw += md;
            us(e.fi,e.se);
        }
    }
    cout << tw;
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if (fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }   
    ll T = 1;
    // cin >> T;
    for (ll i = 1; i <= T; i++) {
        solve();
        cout << '\n';
    }
}

Compilation message

sirni.cpp: In function 'void solve()':
sirni.cpp:50:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for(ll i = 0; i < a.size(); i++) opt[a[i]] = i;
      |                   ~~^~~~~~~~~~
sirni.cpp:54:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     for(ll i = 0; i < a.size()-1; i++){
      |                   ~~^~~~~~~~~~~~
sirni.cpp:45:8: warning: unused variable 'ans' [-Wunused-variable]
   45 |     ll ans = 0;
      |        ^~~
sirni.cpp: In function 'int main()':
sirni.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 149 ms 313680 KB Output is correct
2 Correct 193 ms 370260 KB Output is correct
3 Correct 140 ms 313936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 235604 KB Output is correct
2 Runtime error 613 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 138 ms 313816 KB Output is correct
2 Correct 138 ms 313476 KB Output is correct
3 Correct 136 ms 313940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 264632 KB Output is correct
2 Correct 219 ms 321380 KB Output is correct
3 Correct 158 ms 286380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 247256 KB Output is correct
2 Correct 157 ms 289232 KB Output is correct
3 Correct 137 ms 260800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 288876 KB Output is correct
2 Correct 225 ms 357752 KB Output is correct
3 Correct 156 ms 283568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 244392 KB Output is correct
2 Correct 232 ms 357380 KB Output is correct
3 Correct 151 ms 285928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 341456 KB Output is correct
2 Runtime error 574 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 201 ms 351432 KB Output is correct
2 Runtime error 614 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 147 ms 318536 KB Output is correct
2 Runtime error 650 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -