QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#315499 | #8177. Sum is Integer | ucup-team087# | WA | 204ms | 25300kb | C++20 | 13.1kb | 2024-01-27 13:47:32 | 2024-01-27 13:47:33 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t>
void print_single(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
print_single(p.a,2);
print_single(p.b,suc);
}
template<class T>
void print_single(const vector<T>&v,int suc=1){
rep(i,v.size())
print_single(v[i],i==int(v.size())-1?suc:2);
}
template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
rep(i,v.size())
print_single(v[i]+off,i==int(v.size())-1?suc:2);
}
template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
rep(i,N)
print_single(v[i],i==int(N)-1?suc:2);
}
template<class T>
void print(const T&t){
print_single(t);
}
template<class T,class ...Args>
void print(const T&t,const Args&...args){
print_single(t,2);
print(args...);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
void YES(bool ex=true){
cout<<"YES\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void NO(bool ex=true){
cout<<"NO\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void Yes(bool ex=true){
cout<<"Yes\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void No(bool ex=true){
cout<<"No\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}*/
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
int popcount(ull t){
return __builtin_popcountll(t);
}
int bitparity(ll t){
return __builtin_parityll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
ull umask(int i){
return (ull(1)<<i)-1;
}
ll minp2(ll n){
if(n<=1)return 1;
else return ll(1)<<(topbit(n-1)+1);
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
//#ifdef LOCAL
static mt19937_64 gen;
/*#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif*/
return uniform_int_distribution<ll>(l, r)(gen);
}
ll rand_int(ll k){ //[0,k)
return rand_int(0,k-1);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t,class u>
int lwb(const vc<t>&v,const u&a){
return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
return binary_search(all(v),a);
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
template<class t>
vc<t> presum(const vc<t>&a){
vc<t> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
vc<ll> presum(const vi&a){
vc<ll> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
gnr(i,1,si(a))a[i]-=a[i-1];
return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
int n=si(a),m=si(a[0]);
vvc<ll> b(n+1,vc<ll>(m+1));
rep(i,n)rep(j,m)
b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
return b;
}
//verify してないや
void transvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[j][i]=a[i][j];
}
a.swap(b);
transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[m-1-j][i]=a[i][j];
}
a.swap(b);
rotvvc(n,m,args...);
}
//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
int n=si(a);
vi idx(n);iota(all(idx),0);
sort(all(idx),[&](int i,int j){return a[i]<a[j];});
return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
int n=si(a);
assert(si(idx)==n);
vc<t> vs(n);
rep(i,n)vs[i]=a[idx[i]];
return vs;
}
//CF850C
vi invperm(const vi&p){
int n=si(p);
vi q(n);
rep(i,n)q[p[i]]=i;
return q;
}
template<class t,class s=t>
s SUM(const vc<t>&a){
return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
return accumulate(all(a),s(0));
}
template<class t>
t MAX(const vc<t>&a){
return *max_element(all(a));
}
template<class t>
pair<t,int> MAXi(const vc<t>&a){
auto itr=max_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class A>
auto MIN(const A&a){
return *min_element(all(a));
}
template<class t>
pair<t,int> MINi(const vc<t>&a){
auto itr=min_element(all(a));
return mp(*itr,itr-a.bg);
}
vi vid(int n){
vi res(n);iota(all(res),0);
return res;
}
template<class S>
void soin(S&s){
sort(all(s));
}
template<class S,class F>
void soin(S&s,F&&f){
sort(all(s),forward<F>(f));
}
template<class S>
S soout(S s){
soin(s);
return s;
}
template<class S>
void rein(S&s){
reverse(all(s));
}
template<class S>
S reout(S s){
rein(s);
return s;
}
template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a){return mp(-a.a,-a.b);}
template<class t,class u>
istream&operator>>(istream&is,pair<t,u>&a){
return is>>a.a>>a.b;
}
template<class t>
t gpp(vc<t>&vs){
assert(si(vs));
t res=move(vs.back());
vs.pop_back();
return res;
}
template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
a.insert(a.ed,all(b));
}
template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
(pb(a,forward<Args>(b)),...);
return a;
}
template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
for(auto&v:a)v+=x;
return a;
}
template<class t,class u>
vc<t> operator+(vc<t> a,u x){
return a+=x;
}
template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
vc<t> c(max(si(a),si(b)));
rep(i,si(a))c[i]+=a[i];
rep(i,si(b))c[i]+=b[i];
return c;
}
template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
for(auto&v:a)v-=x;
return a;
}
template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
return a-=x;
}
template<class t,class u>
void remval(vc<t>&a,const u&v){
a.erase(remove(all(a),v),a.ed);
}
//消した要素の個数を返してくれる
//UCUP 2-8-F
template<class t,class F>
int remif(vc<t>&a,F f){
auto itr=remove_if(all(a),f);
int res=a.ed-itr;
a.erase(itr,a.ed);
return res;
}
template<class VS,class u>
void fila(VS&vs,const u&a){
fill(all(vs),a);
}
template<class t,class u>
int findid(const vc<t>&vs,const u&a){
auto itr=find(all(vs),a);
if(itr==vs.ed)return -1;
else return itr-vs.bg;
}
template<class t>
void rtt(vc<t>&vs,int i){
rotate(vs.bg,vs.bg+i,vs.ed);
}
bool dbg=false;
const int vmax=ten(5)+10;
bool pri[vmax];
vi ps;
int sf[vmax]; //smallest factor, sf[1] is undefined(0)
void linear_sieve(){
rng(i,2,vmax)
pri[i]=1;
rng(i,2,vmax){
if(pri[i]){
ps.pb(i);
sf[i]=i;
}
for(int j=0;i*ps[j]<vmax;j++){
pri[i*ps[j]]=0;
sf[i*ps[j]]=ps[j];
if(i%ps[j]==0)break;
}
}
}
int lf[vmax]; //largest factor
void initlf(){
rng(i,2,vmax)if(pri[i])lf[i]=i;
else lf[i]=lf[i/sf[i]];
}
template<class t>
vc<pair<t,int>> to_freq(vc<t> a){
sort(all(a));
vc<pair<t,int>> res;
for(auto v:a){
if(res.empty()||res.back().a!=v)res.eb(v,0);
res.back().b++;
}
return res;
}
template<class t=ll>
t extgcd(t a,t b,t&x,t&y){
if(b==0){
x=1;
y=0;
return a;
}else{
t g=extgcd(b,a%b,y,x);
y-=a/b*x;
return g;
}
}
//x*y=g mod m
//returns (g,y)
//x=0 -> (m,0) (未確認)
template<class t=ll>
pair<t,t> modinv(t x,t m){
t a,b;
t g=extgcd(x,m,a,b);
if(a<0)a+=m/g;
return {g,a};
}
void slv(){
int n;cin>>n;
vc<pi> pq(n);
vvc<int> pos_raw(vmax);
rep(i,n){
int p,q;cin>>p>>q;
int g=gcd(p,q);
p/=g;
q/=g;
pq[i]=pi(p,q);
pos_raw[q].pb(i);
}
vc<ull> dif(n);
mt19937_64 gen(20240127);
for(auto p:ps){
vi pos;
for(int v=p;v<vmax;v+=p)
pb(pos,pos_raw[v]);
if(pos.empty())continue;
dmp(p);
int w=1;
for(auto i:pos){
int x=pq[i].b,e=1;
while(x%p==0){
x/=p;
e*=p;
}
chmax(w,e);
}
int cur=0;
map<int,ull> buf;
buf[0]=0;
for(auto i:pos){
int z;
{
int g=gcd(pq[i].b,w);
int num=pq[i].a*(w/g);
int den=pq[i].b/g;
auto [tmp,val]=modinv(den,w);
assert(tmp==1);
z=(num%w*val)%w;
}
dmp2(i,z);
int nx=(cur+z)%w;
if(!buf.contains(nx)){
buf[nx]=gen();
}
dif[i]^=buf[cur]^buf[nx];
cur=nx;
}
}
vc<ull> sum(n+1);
rep(i,n)sum[i+1]=sum[i]^dif[i];
auto f=to_freq(sum);
int ans=0;
for(auto [key,val]:f)ans+=val*(val-1)/2;
print(ans);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
linear_sieve();
if(dbg){
while(1)slv();
}else{
//int t;cin>>t;rep(_,t)
slv();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 6512kb
input:
4 1 6 1 3 1 2 1 2
output:
2
result:
ok "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 6664kb
input:
5 1 1 2 2 3 3 4 4 5 5
output:
15
result:
ok "15"
Test #3:
score: 0
Accepted
time: 3ms
memory: 6668kb
input:
2 1 99999 99999 100000
output:
0
result:
ok "0"
Test #4:
score: 0
Accepted
time: 27ms
memory: 15924kb
input:
200000 82781 82781 86223 86223 16528 16528 84056 84056 94249 94249 54553 54553 25943 25943 10415 10415 52417 52417 46641 46641 70298 70298 84228 84228 55441 55441 49326 49326 11753 11753 89499 89499 58220 58220 71482 71482 32373 32373 7251 7251 78573 78573 74268 74268 46682 46682 20314 20314 85519 8...
output:
10603308211
result:
ok "10603308211"
Test #5:
score: 0
Accepted
time: 18ms
memory: 16076kb
input:
200000 50741 50741 86798 95775 51104 51104 29372 29372 43295 43295 55065 55065 68947 68947 35282 35282 62467 62467 68481 68481 82613 82613 95921 95921 46302 46302 53806 53806 61244 61244 16078 16078 33476 33476 9084 9084 99273 99273 11678 11678 36816 36816 30311 30311 51479 51479 2667 2667 57043 570...
output:
20066919
result:
ok "20066919"
Test #6:
score: 0
Accepted
time: 49ms
memory: 17440kb
input:
200000 98235 98235 67434 96040 49102 49102 16569 16569 1095 1095 23901 23901 6143 6143 78285 78285 9853 9853 46454 46454 52131 52131 72378 72378 53983 53983 91453 91453 38655 83910 6455 6455 80993 80993 66871 66871 45005 45005 72124 72124 17949 17949 34378 34378 81399 81399 89147 89147 72892 72892 8...
output:
1808373
result:
ok "1808373"
Test #7:
score: 0
Accepted
time: 73ms
memory: 18644kb
input:
200000 64364 74993 65425 91573 10305 10305 31901 31901 90499 95090 13337 47707 32342 38531 75909 93251 95924 95924 12789 12789 77190 77190 82753 99616 33824 79787 48159 48159 32648 32648 90698 98365 89028 89028 36982 36982 11377 11377 79190 88165 23457 23457 24114 24114 55183 71128 65165 65165 4196 ...
output:
593601
result:
ok "593601"
Test #8:
score: 0
Accepted
time: 114ms
memory: 20220kb
input:
200000 42985 42985 30472 30472 4697 50160 91745 95118 77209 77209 32676 32676 96375 96550 18636 18636 93176 93202 27039 27039 2001 85497 74148 94045 82232 92935 71481 80579 99738 99977 90865 90865 93800 99894 11923 64394 29930 29930 40659 40659 12932 24625 47502 47502 34808 52414 37132 37132 78333 8...
output:
200046
result:
ok "200046"
Test #9:
score: 0
Accepted
time: 158ms
memory: 23340kb
input:
200000 38189 83791 82487 82487 42636 69054 46661 46661 55193 83194 40205 87111 29683 29683 79038 79038 98893 99029 1297 1297 29036 29036 14288 14288 92671 94553 93133 96764 20394 27614 51001 51001 95715 99182 57011 64387 40743 84877 53871 53871 9572 36322 52854 52854 48164 48164 54890 90222 13149 23...
output:
66793
result:
ok "66793"
Test #10:
score: 0
Accepted
time: 204ms
memory: 23872kb
input:
200000 88510 92529 59515 86234 77518 89158 30499 67484 21592 66929 22068 85765 56040 80464 47128 86562 72993 74449 47648 55593 41645 65306 93686 98745 93577 97957 63897 92381 99261 99663 14073 53816 84719 96510 35202 78173 8823 28953 6740 40358 34790 66738 22323 83799 8982 93169 41275 70673 93933 99...
output:
2006
result:
ok "2006"
Test #11:
score: 0
Accepted
time: 126ms
memory: 25300kb
input:
200000 37542 94781 52292 94781 41475 94781 64295 94781 19942 94781 20632 94781 23926 32922 15988 32922 70400 94781 33866 79612 73489 94781 11346 32922 29597 94781 18851 32922 75353 79612 10468 18421 68719 94781 30040 80989 27637 94781 9313 21313 26684 47093 10860 79612 2343 63521 2159 32922 4106 470...
output:
2
result:
ok "2"
Test #12:
score: 0
Accepted
time: 121ms
memory: 22924kb
input:
200000 14155 82459 4550 92282 9935 59120 35119 43577 69681 92282 6573 33835 193 59120 2297 88354 33149 59120 21365 59120 14906 59120 4898 71617 802 25637 46411 59120 65015 92282 20057 71617 25342 71617 7440 82459 8525 71617 22625 25637 19203 82459 85531 92282 37402 71617 2660 82459 17454 33835 7833 ...
output:
5
result:
ok "5"
Test #13:
score: -100
Wrong Answer
time: 104ms
memory: 21788kb
input:
200000 1 2 2 3 4 5 3 7 7 11 6 13 9 17 15 19 17 23 12 29 18 31 10 37 23 41 41 43 20 47 24 53 12 59 14 61 63 67 59 71 16 73 42 79 15 83 18 89 12 97 4 101 91 103 13 107 86 109 3 113 53 127 84 131 112 137 22 139 37 149 58 151 153 157 160 163 156 167 41 173 91 179 66 181 29 191 64 193 107 197 91 199 158 ...
output:
4575
result:
wrong answer 1st words differ - expected: '41227', found: '4575'