(模板题)UVALive 7362 Farey(欧拉函数)

来源:互联网 发布:xp映射网络驱动器 编辑:程序博客网 时间:2024/04/19 13:49

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5384

Given a positive integer, N, the sequence of all fractions a/b with (0 < a ≤ b), (1 < b ≤ N) and a andb relatively prime, listed in increasing order, is called the Farey Sequence of order N.

For example, the Farey Sequence of order 6 is:

0/1, 1/6, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 5/6, 1/1

For this problem, you will write a program to compute the length of the Farey sequence of order N(input).

InputThe first line of input contains a single integer P, (1 ≤ P ≤ 10000), which is the number of data setsthat follow.

Each data set should be processed identically and independently.Each data set consists of a single line of input. 

It contains the data set number, K, followed by theorder N, N(2 ≤ N ≤ 10000), of the Farey Sequence whose length is to be found.

OutputFor each data set there is a single line of output. The single output line consists of the data set number,K, followed by a single space followed by the length of the Farey Sequence as a decimal integer.

Sample Input

1 6

2 15

3 57

4 9999

Sample Output

1 13

2 73

3 1001

4 30393487

提示

题意:

有p(1<=p<=10000)组数据,给出一个数n(2<=n<=10000),请求出有几种写成分数的情况。

分数格式:

a/b,且a<b<=n,1/2,2/4,算一种情况,换句话说,a与b两两互质(最大公约数为1)。

思路:

不晓得欧拉函数的只能眼睁睁看人家AC刷刷的往上冒,心痛啊%>_<%。

直接套就行了。

示例程序

#include <stdio.h>int f(int n){    int t=1,i;    for(i=2;n>=i*i;i++)    {        if(n%i==0)        {            n=n/i;            t=t*(i-1);            while(n%i==0)            {                n=n/i;                t=t*i;            }        }    }    if(n>1)    {        t=t*(n-1);    }    return t;}int main(){    int b[10001],i,i1,n,m,t;    b[1]=2;    for(i=2;10000>=i;i++)    {        b[i]=b[i-1]+f(i);    }    scanf("%d",&t);    for(i=1;t>=i;i++)    {        scanf("%d %d",&m,&n);        printf("%d %d\n",m,b[n]);    }    return 0;}

下面一种做法(看上去应该差不多)的是其他人的一份代码:

#include <stdio.h>int main(){    int a[10001],b[10001],i,i1,n,m,t;    a[1]=2;    b[1]=2;    for(i=2;10000>=i;i++)    {        a[i]=i-1;        for(i1=2;i>i1;i1++)        {            if(i%i1==0)            {                a[i]=a[i]-a[i1];            }        }        b[i]=b[i-1]+a[i];    }    scanf("%d",&t);    for(i=1;t>=i;i++)    {        scanf("%d %d",&m,&n);        printf("%d %d\n",m,b[n]);    }    return 0;}
                                             
0 0