HHU winter training 4

A - Neon Sign

The meaning of problems: n a complete graph, each vertex and the remaining sub-side vertex colors blue, red two kinds. Three sides of the same number of triangles required color.

Consider violence O (n ^ 3) will be T, so out of the inverse solution of the test (in different colors on both sides ans-), or a T
Solution: For a triangle composed of different color fringing, may have only two sides of the same color, a side of the case different colors. If the enumeration over each point, even from recording this point how many sides as the color of edges, denoted sum sum, then a different color for any triangle, its edge piece of a different color two endpoints each will be counted once. Therefore, the number of different colors of the triangle on the 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

Meaning of the questions: who from birth have physical, emotional and intellectual three menstrual cycles, respectively 23, 28 and 33 days. Within a period of one day of peak. This is usually the peak period of three will not be the same day. Now given three dates, corresponding to the date of physical, emotional, intellectual peak appearance. Then gives a start date, required From this day, we calculate the number of days least another three peaks occur simultaneously. ,

Solution: Chinese remainder theorem stencil title, because this question fill the knowledge in this area, see the 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

The meaning of problems: ar given k represents each group x ≡ r (mod a)

:( solution to a problem is not relatively prime) extended Chinese remainder theorem, in fact, it does not matter with the Chinese remainder theorem ,
see the expansion of the Chinese remainder 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;
}
Published 24 original articles · won praise 5 · Views 4276

Guess you like

Origin blog.csdn.net/qq_43640009/article/details/104360048