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

题目链接: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值