ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1846. GCD 2010

Posted by Hunarmand 5 Mar 2019 15:46
WA5! why?

code:

# include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b)
{
int c;
if(a < b)
swap(a, b);
while(1)
{
a = a % b;
if(a == 0)
return b;
c = a;
a = b;
b = c;
}
}

set <int> gtx;
map <int, int> bot;
map <set <int>, int> gg;

int main()
{
ios_base::sync_with_stdio(0);
int q, a, s, i;
char x;
cin >> q;

for(i = 1; i <= q; i++)
{
cin >> x >> a;
if(x == '+')
{
bot[a]++;
gtx.insert(a);

if(gg[gtx])
cout << gg[gtx] << '\n';
else if(gtx.size() == 1)
{
s = a;
gg[gtx] = s;
cout << s << '\n';
}
else
{
s = gcd(s, a);
gg[gtx] = s;
cout << s << '\n';
}
}
else
{
bot[a]--;
if(bot[a] == 0)
gtx.erase(a);
if(gtx.size() == 0)
{
cout << 1 << '\n';
continue;
}
if(gg[gtx])
cout << gg[gtx] << '\n';
else
{
set <int> :: iterator j = gtx.begin();
s = *j;
for( ; j != gtx.end(); j++)
s = gcd(s, *j);
gg[gtx] = s;
cout << s << '\n';
}
}
}
return 0;
}

Edited by author 05.03.2019 16:25