A - Neon Sign
Meaning: the complete graph of n points. The colors of each vertex and other vertex edges are blue and red. The number of triangles with the same color on three sides is required.
Considering that violence O(n^3) can T, so I tried to find a counter solution (if the colors on both sides are different, then ans -), or T
Solution: for a triangle composed of different color edges, there may only be two edges of the same color and one edge of different colors. If you enumerate each point, record how many edges from this point are of different colors, and sum them up as sum, then for any triangle with different colors, the two ends of that different color edge will be calculated once respectively. So the number of triangles of different colors is sum/2.
#include <iostream> #include <cstring> using namespace std; int n; int a[1005],b[1005]; int main(){ ios::sync_with_stdio(0); int T; cin>>T; while (T--){ int n,x; cin>>n; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int ans=0; for (int i=1; i<=n-1; i++){ for (int j=i+1; j<=n; j++){ cin>>x; if (x) { a[i]++; a[j]++; } else { b[i]++; b[j]++; } } } for (int i=1; i<=n; i++){ ans+=a[i]*b[i]; } ans=n*(n-1)*(n-2)/6-ans/2; cout<<ans<<endl; } return 0; }
C - Biorhythms
Since birth, people have three physiological cycles of physical strength, emotion and intelligence, which are 23, 28 and 33 days respectively. One day in a cycle is the peak. Generally, the peaks of these three periods will not be the same day. Now, three dates are given, corresponding to the peak dates of physical strength, emotion and intelligence. Then we give a starting date, from which we need to calculate the minimum number of days after three peaks appear at the same time. ,
Solution: China's residual theorem template problem, because this problem complements this knowledge, see Chinese remainder theorem
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; ll a[4]; ll m[4]={23,28,33}; const int mod=21252; ll extend_gcd(ll a, ll b, ll &x, ll &y) { ll res=a; if (b!=0) { res=extend_gcd(b,a%b,y,x); y-=(a/b)*x; } else { x=1; y=0; } return res; } ll china(int len, ll *m, ll *a) { ll M=1,w,d,x,y,ret=0; for (int i=0; i<len; i++) M*=m[i]; for (int i=0; i<len; i++) { w=M/m[i]; d=extend_gcd(m[i],w,x,y); ret=(ret+y*w*a[i])%M; } return (M+ret%M)%M; } int main() { //ios::sync_with_stdio(0); ll d; int cnt=1; while (cin>>a[0]>>a[1]>>a[2]>>d) { if(a[0]==-1 && a[1]==-1 && a[2]==-1 && d==-1) break; ll ans=(china(3,m,a)-d)%mod; if (ans<=0) ans+=mod; printf("Case %d: the next triple peak occurs in %lld days.\n",cnt++,ans); } return 0; }
E - Strange Way to Express Integers
What's the meaning of the question: each group of k group a r represents x ≡ r (mod a)
The expansion of China's surplus theorem has nothing to do with China's surplus theorem,
See for details Expanding China's surplus theorem
#include <iostream> #include <cstdio> typedef long long ll; using namespace std; ll m[60000],a[60000]; ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; } ll extend_gcd(ll a, ll b, ll &x, ll &y) { ll res=a; if (b!=0) { res=extend_gcd(b,a%b,y,x); y-=(a/b)*x; } else { x=1; y=0; } return res; } ll exchina(ll n) { ll m1=m[0],a1=a[0]; ll m2,a2,k1,k2,x0,g,c; ll lcm=m[0]; for(int i=1; i<n; i++) { m2=m[i]; a2=a[i]; c=a2-a1; g=extend_gcd(m1,m2,k1,k2); lcm=lcm*m[i]/gcd(lcm,m[i]); if(c%g) return -1; x0=k1*c/g; ll t=m2/g; x0=(x0%t+t)%t; a1+=m1*x0; m1=t*m1; } if (a1==0){ a1=1; for (int i=0; i<n; i++) a1=a1*m[i]/gcd(a1,m[i]); } return a1; } int main() { ios::sync_with_stdio(0); ll n; while (cin>>n) { for (int i=0; i<n; i++) { cin>>m[i]>>a[i]; } if (n==1) { cout<<a[0]<<endl; continue; } ll ans=exchina(n); cout<<ans<<endl; } return 0; }