1. 程式人生 > >Codeforces Round #511 (Div. 2) A 構造B 數學C素數篩

Codeforces Round #511 (Div. 2) A 構造B 數學C素數篩

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 ; 
}