Codeforces Round #511 (Div. 2) A 构造B 数学C素数筛

版权声明:本文为博主原创文章,未经博主允许也可以转载。 https://blog.csdn.net/FrankAx/article/details/82830680

A
题意:构造a+b+c == n 且 a , b , c 都不为3的倍数。
思路:用1,2去构造即可。
Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n ;
	cin >> n ; 
	if( n % 3 == 0 ){
		cout << n - 2 << ' ' << 1 << ' ' << 1 << endl;
	}else{
		if( n % 3 == 1 ){
			cout << n - 2 << ' ' << 1 <<' ' << 1 << endl;
		}else{
			cout << n - 3 << ' ' << 2 << ' ' << 1 << endl;
		}
	}
	return 0 ; 
}

B
题意:等腰三角形放在坐标轴上,给n个坐标,第一象限的点。求等腰三角形最短边的长度最小多少,能够覆盖所有点。
思路:最短边就是腰的长度,斜边方程式 : y = a - x . a为腰长,那么答案就是x+y最大值
Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n ;
	cin >> n ; 
	int res = 0;
	int x , y ;
	for( int i = 0 ; i < n ; i++ ){
		cin >> x >> y ;
		res = max( res , x + y );
	}
	cout << res << endl;
	return 0 ; 
}

C
题意:给n个数,gcd为d,求最少去掉几个数能够使其gcd大于d
思路:每个数都除以d并记录出现次数之后, 枚举素数p,找p的倍数的最大集合,
n-这个集合的数的个数就是去掉的数的个数。
Code:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int AX = 1.5e7+5 ;
int res ;
int mp[AX];
int prime[AX] ; 
int x[300005] ;
int d ;
int gcd( int a , int b ){
	return !b ? a : gcd( b , a % b ) ;
}
int maxn ; 
void getFac(){
	for( int i = 2 ; i < AX ; i++ ){
		if( prime[i] ) continue ;
		int ans = 0 ;
		for( int j = i ; j < AX ; j += i ){
			ans += mp[j] ; 
			prime[j] = 1 ; 
		}
		res = max( res , ans ) ;
	}
}
int main(){
	int n ;
	scanf("%d",&n);
	d = 0 ;
	for( int i = 0 ; i < n ; i++ ){
		scanf("%d",&x[i]) ;
		d = gcd( d , x[i] ) ; 
	}
	for( int i = 0 ; i < n ; i++ ){
		mp[x[i]/d] ++ ; 
	}
	res = 0 ;
	getFac() ; 
	if( !res ) printf("-1\n");
	else printf("%d\n",n-res);
	return 0 ; 
}

猜你喜欢

转载自blog.csdn.net/FrankAx/article/details/82830680