2019牛客暑期多校训练营(第九场)
The power of Fibonacci
Quadratic equation
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1000000007; ll pow_mod(ll a,ll b) { ll ret=1; while (b) { if (b&1) { ret=ret*a%mod; } a=a*a%mod; b>>=1; } return ret; } ll modsqr(ll a) { ll x=pow_mod(a,(mod+1)/4); if (x*x%mod!=a) return -1; return x; } int main() { int _; ll b,c; scanf("%d",&_); ll inv2=pow_mod(2,mod-2); while (_--) { scanf("%lld%lld",&b,&c); ll x=modsqr((b*inv2%mod*b%mod*inv2%mod-c+mod)%mod); if (x==-1) { printf("-1 -1\n"); continue; } x=(x+b*inv2%mod)%mod; ll y=(b-x+mod)%mod; if (x>y) { swap(x,y); } printf("%lld %lld\n",x,y); } return 0; }
Knapsack Cryptosystem
#pragma GCC optimize(2) #include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[100],S,n,c[100],sum,b[800000],ans1,ans2; map<ll,ll>mp; int main() { scanf("%d%lld",&n,&S); for(register int i=1; i<=n; ++i) { scanf("%lld",&a[i]); } int mid=n>>1; for (int i=0; i<(1<<mid); i++) { sum=0; for (int j=0; j<mid; j++) { if ((i>>j)&1) { sum+=a[mid-j]; } } if (S>=sum) { mp[S-sum]=i+1; } } int nn=n-mid; for (int i=0; i<(1<<nn); i++) { sum=0; for (int j=0; j<n-mid; j++) { if ((i>>j)&1) { sum+=a[n-j]; } } if (mp[sum]>0) { ans1=mp[sum]-1; ans2=i; break; } } for (int i=0; i<mid; i++) { if ((ans1>>i)&1) { c[mid-i]=1; } } for (int i=0; i<n-mid; i++) { if ((ans2>>i)&1) { c[n-i]=1; } } for (int i=1; i<=n; i++) { printf("%d",c[i]); } printf("\n"); }
All men are brothers
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ull; const int maxn=100100; ull c2,c4,tmp,a,b; int f[maxn],siz[maxn],n,m,kk; int get_f(int x) { return x==f[x]?x:f[x]=get_f(f[x]); } void merge1(int x,int y) { if (siz[x]>siz[y]) { f[y]=x; siz[x]+=siz[y]; } else { f[x]=y; siz[y]+=siz[x]; } } int main() { scanf("%d%d",&n,&m); for (int i=1; i<=n; i++) { siz[i]=1; f[i]=i; } c2=1ull*n*(n-1)/2; c4=1ull*n*(n-1)/2*(n-2)/3*(n-3)/4; printf("%llu\n",c4); for (int i=1; i<=m; i++) { int x,y; scanf("%d%d",&x,&y); x=get_f(x); y=get_f(y); if (n-kk<4) { printf("0\n"); continue; } if (x==y) { printf("%llu\n",c4); continue; } kk++; a=siz[y]; b=siz[x]; c2-=a*b; tmp=a*b*(c2-(a+b)*(n-a-b)); c4-=tmp; printf("%llu\n",max(0ull,c4)); merge1(x,y); } }
Symmetrical Painting
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll>PII; vector<PII>a; int main() { int n; scanf("%d",&n); for (int i=1; i<=n; i++) { ll l,r; scanf("%lld%lld",&l,&r); a.push_back(make_pair(l+l,1ll)); a.push_back(make_pair(l+r,-2ll)); a.push_back(make_pair(r+r,1ll)); } sort(a.begin(),a.end()); ll last=0,sum=0,ans=0,se=0; for (vector<PII>::iterator it=a.begin(); it!=a.end(); it++) { sum+=se*(it->first-last); ans=max(ans,sum); se+=it->second; last=it->first; } printf("%lld\n",ans); }