This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DIM 1000010
using namespace std;
long long a[DIM],b[DIM],x[DIM],p[22];
int n,i;
int verif(){
for (;;){
int ok2 = 0;
for (i=1;i<=n;i++){
/// trimit cat de mult pot
if (2 * x[i] <= a[i]){
if (x[i])
ok2 = 1;
a[i] -= 2 * x[i];
a[ (i < n) ? (i+1) : (1)] += x[i];
x[i] = 0;
} else {
long long val = a[i] / 2;
if (val)
ok2 = 1;
a[i] -= 2 * val;
a[(i < n) ? (i+1) : (1)] += val;
x[i] -= val;
}
}
if (!ok2)
break;
}
int ok = 1;
for (i=1;i<=n;i++)
if (x[i] != 0 || a[i] != b[i])
ok = 0;
return ok;
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
long long sum = 0, sum2 = 0;
for (i=1;i<=n;i++){
cin>>a[i]>>b[i];
sum += a[i];
sum2 += b[i];
}
if (!sum2){
if (!sum)
cout<<"Yes";
else cout<<"No";
return 0;
}
/// ce porcarie
for (;;){
int ok = 0;
for (i=1;i<=n;i++){
if (a[i] <= b[i])
continue;
long long val = (a[i] - b[i] + 1) / 2;
if (val)
ok = 1;
a[i] -= 2*val;
a[(i < n) ? (i+1) : (1)] += val;
}
if (!ok)
break;
}
int ok = 1;
for (i=1;i<=n;i++)
if (a[i] != b[i]){
ok = 0;
break;
}
if (ok)
cout<<"Yes";
else cout<<"No";
return 0;
if (n == 2){
long long val = a[1] - b[1] + 2 * (a[2] - b[2]);
if (val % 3){
cout<<"No";
return 0;
}
x[2] = val / 3;
if ( (a[1] - b[1] + x[2]) % 2){
cout<<"No";
return 0;
}
x[1] = (a[1] - b[1] + x[2]) / 2;
if (x[1] < 0 || x[2] < 0){
cout<<"No";
return 0;
}
if (verif())
cout<<"Yes";
else cout<<"No";
return 0;
}
p[0] = 1;
for (i=1;i<=20;i++)
p[i] = p[i-1] * 2;
long long val = p[n-1] * (a[1] - b[1]) + p[n-2] * (a[n] - b[n]);
for (i=n-1;i>=2;i--)
val += p[i-2] * (a[i] - b[i]);
if (val % (p[n]-1)){
cout<<"No";
return 0;
}
x[1] = val / (p[n]-1);
if (val < 0){
cout<<"No";
return 0;
}
for (i=2;i<=n;i++){
long long val = a[i] - b[i] + x[i-1];
if (val % 2 || val < 0){
cout<<"No";
return 0;
}
x[i] = val / 2;
}
for (i=1;i<=n;i++){
long long val = a[i] + x[ (i > 1) ? (i-1) : (n) ] - 2 * x[i];
if ( val != b[i] ){
cout<<"No";
return 0;
}
}
if (verif())
cout<<"Yes";
else cout<<"No";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |