A problem of codeforces round 388 Div2
A. Bachgold Problem
The question asks how many prime numbers are added to the input of N. Prime can be reused, then this problem with greed, starting from 2, because you ask the most primes, it is certainly the smaller the number of the more, done, suddenly found not to hit the table, n>=2, or odd, or even, even 2, The odd number of a 2 to 3 on it, it is so simple .... But at that time is really a flash out of prime number, this is not the brain to apply the habit of thinking ah, should be good to each problem to analyze the topic characteristics.
First time to do AC code
#include <iostream> #include <cstring> using namespace std; typedef long LONG int LL
;
const int MAXN = 100005;
BOOL p[maxn];//0 means prime int PRIMENUM[MAXN];
void Setprime () {memset (primenum,0,sizeof (primenum));
memset (P,0,sizeof (p));
for (ll I=2;i<maxn;++i) {if (!p[i]) {for (ll j=i*i;j<maxn;j+=i) p[j]=1;
int main () {Setprime ()}}
int n;
scanf ("%d", &n);
int ans = 0;
for (int i=2;i<=n;++i) {if (!p[i] && n-i>=0) {primenum[i] = n/i;
Ans = ans + primenum[i];
n = n-n/i*i;
} if (n>0) {if (Primenum[n]) {ans++;
primenum[n]++;
}else{//n==1 primenum[2]--;
primenum[3]++;
} printf ("%d\n", ans);
for (int i=2;i<maxn;++i) {while (primenum[i]--) printf ("%d", I);
printf ("\ n");
return 0; }
After discovering the odd-even rule, it becomes a linear time from the table.
#include <iostream>
using namespace std;
typedef long Long int ll;
const int MAXN = 100005;
int main ()
{
int n;
scanf ("%d", &n);
int ans = N/2;
printf ("%d\n", ans);
int two = N/2,three = 0;
if (n%2) two--, three++;
while (two--) printf ("2");
while (three--) printf ("3");
printf ("\ n");
return 0;
}
Time has changed from 31ms to 30ms???? is printf slow. , interested friends can switch to Putchar to try.
Now that we've talked about prime numbers, it's a bit of a way to record near-linear, efficient table-marking.
#include <iostream> #include <string.h> #include <stdio.h> #define MAXN
10000000 BOOL visit[maxn+1000000]; int prime[maxn],n; The size of the prime may be estimated to open the array again.
Presumably (x/lnx) void Getprime () {memset (visit, false, sizeof (visit));
int num = 0;
for (int i = 2; I <= n; ++i) {if (!visit[i]) prime[++num] = i; for (int j = 1; (J <= Num) && (i * prime[j] <= N);
J + +) {visit[I * prime[j]] = true;
A composite composed of I and Prime[j] is given true, noting that I may not be prime, but prime[j] is a prime if (i% prime[j] = = 0) break; /* Here is the focus, to avoid a lot of repeated judgments, such as i=9, now Prime is 2,3,5,7, into the second cycle, visit[2*9]=1;visit[3*9]=1; this time 9%3==0, to jump out. Since 5*9 can be replaced with 3*15, if this is the case, the i=15 will be recalculated again, so this avoids repeating a lot of operations.
* [}} int main () {scanf ("%d", &n);
Getprime ();
return 0; }