10820 - Send a Table
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1761
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 our 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 Output for Sample Input
2
5
0
Sample output
3
19
这是一个裸的Farey数列。下面先介绍一下Farey数列
说到这个数列,就要谈到互质的概念,这个性质导致了这个数列及那个SB(-_-)树的优美的特性。在正式讲Farey前,先考虑一个问题:给出任意一个实数(有理数、无理数),与其最相近的分数是多少。对于一个有理数,我们自然知道有唯一的分数相对应,而无理数则是无限的接近,无法相等,那么怎么找这个分数呢?看完了farey,一切豁然开朗。
来看这样一棵树:
发现其特性了吗,比如1/4,是(0+1/1+3)也就是说是上一层中与其“相邻”的2个分数,分别分子加分子,分母加分母形成。对于每一个farey数列,比如F7=0/1 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7 1/1 这个数列的分数是从小到大排列的,并且分数m/n gcd(m,n)=1.
为什么呢,首先它是有序的,因为每一个分数由上一次相邻的分数产生,并“插入”当前一层,那么m/n<=(m+m’)/(n+n’)<=m’/n’,所以产生的分数必然是有序的。
对于gcd(a,b)=1=>ax-by=1 如果m/n m’/n’可以得到m’n-m’n=1,那么(m+m’)n-m(n+n’)=1 m’(n+n’)-(m+m’)n’=1,而对于数列的最初1*1-0*0=1,所以数学归纳法可得,在上图的树中的相邻的分数产生的分数的分子分母是互质的,并且有序的。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 50010
#define ll long long
using namespace std;
ll a[N];
ll sum[N];
void eular()
{
int i,j;
memset(a,0,sizeof(a));
for(i=2;i<N;i++)
{
if(!a[i])
{
for(j=i;j<=N;j+=i)
{
if(!a[j])
a[j]=j;
a[j]-=a[j]/i;
}
}
}
for(i=2;i<N;i++)
{
sum[i]=sum[i-1]+a[i];
}
}
int main()
{
eular();
int n;
while(scanf("%d",&n),n)
{
printf("%lld\n",sum[n]*2+1);
}
return 0;
}