Submission #409551

#TimeUsernameProblemLanguageResultExecution timeMemory
409551LptN21Sirni (COCI17_sirni)C++14
140 / 140
4510 ms707980 KiB
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e15
#define eps 1e-9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 1e5+7, M=1e7+1;
const int MOD = 1e9+7;

int n, m, k, t;

vector<int> a;
vector<ii> e[M];
int dsu[N];

int root(int u) {return (dsu[u]<0?u:dsu[u]=root(dsu[u]));}
bool join(int u, int v) {
    if((u=root(u))==(v=root(v))) return 0;
    dsu[u]+=dsu[v], dsu[v]=u;return 1;
}

signed main() {
    //freopen("test.inp", "r", stdin);
    //freopen("test.out", "w", stdout);
    //fastIO;
    scanf("%d", &n);a.assign(n, 0);int ans=0;
    for(int i=0;i<n;i++) scanf("%d", &a[i]);
    sort(a.begin(), a.end()), a.resize(unique(a.begin(), a.end())-a.begin());
    memset(dsu, -1, sizeof(int)*(sz(a)+1));
    for(int i=0;i<sz(a);i++)
        for(int j=a[i];j<M;j+=a[i]) {
            k=lb(a.begin(), a.end(), j+(j==a[i]))-a.begin();
            if(k==sz(a)) break;
            e[a[k]%a[i]].pb(ii(i+1, k+1));
        }
    for(int i=0;i<M;i++)
        for(int j=0;j<sz(e[i]);j++)
            if(join(e[i][j].FF, e[i][j].SS))
                ans+=i;
    printf("%d", ans);
    return 0;
}
/* stuff you should look for
    - int overflow, array bounds
    - special cases (n=1?)
    - do smth instead of do nothing and stay organized
    - WRITE STUFF DOWN
    - DONT JUST STICK ON ONE APPROACH
*/

Compilation message (stderr)

sirni.cpp: In function 'int main()':
sirni.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |     scanf("%d", &n);a.assign(n, 0);int ans=0;
      |     ~~~~~^~~~~~~~~~
sirni.cpp:36:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     for(int i=0;i<n;i++) scanf("%d", &a[i]);
      |                          ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...