UVA 10820 Send a Table [欧拉函数] [线性筛法]

来源:互联网 发布:新菜鸟网络俱乐部 编辑:程序博客网 时间:2024/06/05 17:00

Send a Table
Time Limit: 3000MS
Memory Limit: Unknown
64bit IO Format: %lld & %llu

When participating in programming contests, you sometimes face the following problem: You know
how to calcutale the output for the given input values, but your algorithm is way too slow to ever
pass the time limit. However hard you try, you just can’t discover the proper break-off conditions that
would bring down the number of iterations to within acceptable limits.
Now if the range of input values is not too big, there is a way out of this. Let your PC rattle for half
an hour and produce a table of answers for all possible input values, encode this table into a program,
submit it to the judge, et voila: Accepted in 0.000 seconds! (Some would argue that this is cheating,
but remember: In love and programming contests everything is permitted).
Faced with this problem during one programming contest, Jimmy decided to apply such a ’technique’.
But however hard he tried, he wasn’t able to squeeze all his pre-calculated values into a program
small enough to pass the judge. The situation looked hopeless, until he discovered the following property
regarding the answers: the answers where calculated from two integers, but whenever the two
input values had a common factor, the answer could be easily derived from the answer for which the
input values were divided by that factor. To put it in other words:
Say Jimmy had to calculate a function Answer(x, y) where x and y are both integers in the range
[1, N]. When he knows Answer(x, y), he can easily derive Answer(k ∗ x, k ∗ y), where k is any integer
from it by applying some simple calculations involving Answer(x, y) and k.
For example if N = 4, he only needs to know the answers for 11 out of the 16 possible input value
combinations: Answer(1, 1), Answer(1, 2), Answer(2, 1), Answer(1, 3), Answer(2, 3), Answer(3, 2),
Answer(3, 1), Answer(1, 4), Answer(3, 4), Answer(4, 3) and Answer(4, 1). The other 5 can be derived
from them (Answer(2, 2), Answer(3, 3) and Answer(4, 4) from Answer(1, 1), Answer(2, 4) from
Answer(1, 2), and Answer(4, 2) from Answer(2, 1)). Note that the function Answer is not symmetric,
so Answer(3, 2) can not be derived from Answer(2, 3).
Now what we want you to do is: for any values of N from 1 upto and including 50000, give the
number of function Jimmy has to pre-calculate.

Input
The input file contains at most 600 lines of inputs. Each line contains an integer less than 50001 which
indicates the value of N. Input is terminated by a line which contains a zero. This line should not be
processed.

Output
For each line of input produce one line of output. This line contains an integer which indicates how
many values Jimmy has to pre-calculate for a certain value of N.

Sample Input
2
5
0

Sample Output
3
19


求范围内 x , y 互质的个数 ANS = 2*f(n) + 1
x == y 时 也算一种情况

O(nloglogn) Eratosthenes筛法

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 50000;int phi[maxn + 5];int sum[maxn + 5];void build_phi(){    phi[1] = 1;    for(int i=2;i<=maxn;i++) if(!phi[i])        for(int j=i;j<=maxn;j+=i)        {            if(!phi[j]) phi[j] = j;            phi[j] = phi[j] / i * (i-1);        }    for(int i=1;i<=maxn;i++) sum[i] = sum[i-1] + phi[i];}int main(){    freopen("table.in","r",stdin);    freopen("table.out","w",stdout);    build_phi();    int n;    while(~scanf("%d",&n) && n)    {        int ans = sum[n] - sum[1];        ans = ans<<1|1;        printf("%d\n",ans);    }    return 0;}

O(n) 欧拉筛法 , 每个合数只会计算一次 不存在多次访问

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 50000;bool no[maxn];int prime[maxn>>3]; // n / ln(n)int prime_cnt;int phi[maxn + 5];int sum[maxn + 5];void build_phi(){    phi[1] = 1;    prime_cnt = 0;    for(int i=2;i<=maxn;i++)    {        if(!no[i])            prime[++prime_cnt]=i,phi[i]=i-1;        int cur = 1;        int to = i * prime[cur];        while(cur<=prime_cnt && to <= maxn)        {            no[to]=true;            if(i%prime[cur] == 0) { phi[to] = phi[i] * prime[cur]; break; }            else phi[to] = phi[i] * phi[prime[cur]]; // based on i!!            to = i * prime[++cur];        }    }    for(int i=1;i<=maxn;i++) sum[i] = sum[i-1] + phi[i];}int main(){    freopen("table.in","r",stdin);    freopen("table.out","w",stdout);    build_phi();    int n;    while(~scanf("%d",&n) && n)    {        int ans = sum[n] - sum[1];        ans = ans<<1|1;        printf("%d\n",ans);    }    return 0;}
0 0