10820 - Send a Table
Posted: Sat Mar 05, 2005 9:25 pm
Seems literally everyone was able to solve A...any hints would be appreciated....
Code: Select all
1
100
1000
10000
50000
Code: Select all
1
5647
550769
54866397
3089251479
Correct output:sumankar wrote:Can someone give output for the following :Code: Select all
1 100 1000 10000 50000
Code: Select all
1
6087
608383
60794971
1519848527
Look - up Tables -huh?Have the totient functions calculated for all nObserver wrote:Tried 20 more times and get down to 0.00.008... Dunno how to optimize it anymore. Maybe some look-up tables? (Dun even know what to put in the table)![]()
No it was, believe me.P.S. sumankar, sorry if my reply wasn't that useful...![]()
Let's hope he comes down and explains here ...P.P.S. Lawrence (..) has just achieved 0.00.000! ho ging ah~~~
Please try it then ~sumankar wrote:Look - up Tables -huh?Have the totient functions calculated for all n and calculating totient functions would mean the answers can be calculated easily - store them in a table and go on printf'ing happily ever after.
Code: Select all
#include <stdio.h>
//using namespace std;
int p[50001];
int pc[50001];
long f[50001];
void PrepareArray()
{
long j;
for (long i = 2; i <= 50000; i++) {
pc[i] = pc[i-1];
if (p[i]) continue;
pc[i] = pc[i] + 1;
j = 2 * i;
for (; j <= 50000; j=j+i) p[j]++;
}
}
void main()
{
for (long i = 1; i <= 50000 ; i++) p[i] = 0;
for (i = 1; i <= 50000 ; i++) pc[i] = 0;
for (i = 1; i <= 50000 ; i++) f[i] = 0;
PrepareArray();
long max = 1;
f[1] = 1;
int N = 0;
while (scanf("%d\n",&N)==1){
if (N > max) {
for (i = max + 1; i <= N; i++)
if (p[i]) { f[i] = f[i-1] + (pc[i] - p[i] + 1)*2; }
else { f[i] = f[i-1] + (i - 1) * 2; }
max = N;
}
printf("%d\n",f[N]);
}
}
Code: Select all
#include <cstdio>
using namespace std;
Code: Select all
for (long i = 1; i <= 50000 ; i++) p[i] = 0;
for (i = 1; i <= 50000 ; i++) pc[i] = 0;
Code: Select all
long i;
for (i = 1; i <= 50000 ; i++) p[i] = 0;
for (i = 1; i <= 50000 ; i++) pc[i] = 0;