Cod sursa(job #2787003)

Utilizator dnprxDan Pracsiu dnprx Data 22 octombrie 2021 11:30:03
Problema Congr Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;

char s[5000005];
int a[300003], b[300003], na, nb, p;
int t[600003], k;
ifstream fin("congr.in");
ofstream fout("congr.out");

int main()
{
    int i, x, y, suma = 0;
    fin >> p;
    fin.get();
    fin.getline(s, 5000001);
    /// pun in a[] primele p numere
    /// si in b[] ultimele p-1 numere
    for (i = 0; s[i]; )
        if (s[i] == ' ') i++;
        else
        {
            x = 0;
            while ('0' <= s[i] && s[i] <= '9')
            {
                x = x * 10 + s[i] - '0';
                i++;
            }
            t[++k] = x % p;
            if (k <= p)
            {
                a[na++] = k;
                suma = (suma + x) % p;
            }
            else b[nb++] = k;
        }

    srand(time(0));
    while (suma != 0)
    {
        x = rand() % p;
        y = rand() % (p - 1);
        suma = (suma - t[a[x]] + t[b[y]] + p) % p;
        swap(a[x], b[y]);
    }

    for (i = 0; i < p; i++)
        fout << a[i] << " ";
    fout.close();
    return 0;
}