665D - Simple Subset - CodeForces Solution


constructive algorithms greedy number theory *1800

Please click on ads to support us..

Python Code:

import sys
readline=sys.stdin.readline

class Prime:
    def __init__(self,N):
        assert N<=10**8
        self.smallest_prime_factor=[None]*(N+1)
        for i in range(2,N+1,2):
            self.smallest_prime_factor[i]=2
        n=int(N**.5)+1
        for p in range(3,n,2):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
                for i in range(p**2,N+1,2*p):
                    if self.smallest_prime_factor[i]==None:
                        self.smallest_prime_factor[i]=p
        for p in range(n,N+1):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
        self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]]

    def Factorize(self,N):
        assert N>=1
        factors=defaultdict(int)
        if N<=len(self.smallest_prime_factor)-1:
            while N!=1:
                factors[self.smallest_prime_factor[N]]+=1
                N//=self.smallest_prime_factor[N]
        else:
            for p in self.primes:
                while N%p==0:
                    N//=p
                    factors[p]+=1
                if N<p*p:
                    if N!=1:
                        factors[N]+=1
                    break
                if N<=len(self.smallest_prime_factor)-1:
                    while N!=1:
                        factors[self.smallest_prime_factor[N]]+=1
                        N//=self.smallest_prime_factor[N]
                    break
            else:
                if N!=1:
                    factors[N]+=1
        return factors

    def Divisors(self,N):
        assert N>0
        divisors=[1]
        for p,e in self.Factorize(N).items():
            pow_p=[1]
            for _ in range(e):
                pow_p.append(pow_p[-1]*p)
            divisors=[i*j for i in divisors for j in pow_p]
        return divisors

    def Is_Prime(self,N):
        return N==self.smallest_prime_factor[N]

    def Totient(self,N):
        for p in self.Factorize(N).keys():
            N*=p-1
            N//=p
        return N

    def Mebius(self,N):
        fact=self.Factorize(N)
        for e in fact.values():
            if e>=2:
                return 0
        else:
            if len(fact)%2==0:
                return 1
            else:
                return -1

N=int(readline())
A=list(map(int,readline().split()))
Pr=Prime(2*10**6)
cnt=A.count(1)
for a in A:
    if a==1:
        continue
    if Pr.Is_Prime(a+1) and cnt:
        ans=cnt+1
        ans_lst=[1]*cnt+[a]
        break
else:
    if cnt>=2:
        ans=cnt
        ans_lst=[1]*cnt
    else:
        for i in range(N):
            for j in range(i+1,N):
                if Pr.Is_Prime(A[i]+A[j]):
                    ans=2
                    ans_lst=[A[i],A[j]]
                    break
            else:
                continue
            break
        else:
            ans=1
            ans_lst=[A[0]]
print(ans)
print(*ans_lst)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using PII=pair<int,int>;

const int N=1e6+10;
int n;
int a[1010],t[N];
int cnt,num2,idx;
bool check(int x){
	if(x==2) return 1;
	for(int i=2,j=sqrt(x);i<=j;i++)
		if(x%i==0) return 0;
	return 1;
}
void tong(){
	for(int i=1;i<=N;i++){
		if(t[i]) a[++idx]=i;
	}
}
void solve(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		if(a[i]==1) cnt++;
		if(check(a[i]+1) && a[i]!=1) num2=a[i];
		t[a[i]]++;
	}
	if(cnt==1 && num2){cout << 2 << endl << 1 << " " << num2; return ;}
	if(cnt>=2){
		if(num2){
			cout << cnt+1 << endl;
			while(cnt--) cout << 1 << " ";
			cout << num2;
		}else{
			cout << cnt << endl;
			while(cnt--) cout << 1 << " ";
		}
		return ;
	}
	tong();
	for(int i=1;i<=idx;i++){
		for(int j=1;j<=idx;j++){
			if(i==j) continue;
			if(check(a[i]+a[j])){cout << 2 << endl << a[i] << " " << a[j]; return ;}
		}
	}
	cout << 1 << endl << a[1] << endl;
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t=1;
	//cin >> t;
	while(t--) solve();
	return 0;
}
//make it count
//开ll了没


Comments

Submit
0 Comments
More Questions

Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game