POJ2407 POJ2478 POJ3090----素数筛&欧拉函数の应用

POJ2407 ---- Relatives

Time Limit: 1000MS Memory Limit: 65536K

Description
Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.

Input
There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

Output
For each test case there should be single line of output answering the question posed above.

Sample Input

7
12
0

Sample Output

6
4
说白了这题就是求欧拉函数 φ ( n ) φ(n) φ(n)

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
typedef long long ll;
ll phi[1000005];

void euler_table(int n)
{
    int i,j;
    memset(phi,0,sizeof(phi));
    for (i=1;i<=n;i++)
        phi[i]=i;
    for (i=2;i<=n;i++)
    {
        if (phi[i]==i)
        {
            for (j=i;j<=n;j+=i)
            {
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}

int main()
{
    int n;
    //打表
    euler_table(1000000);
    int i;
    ll sum; //一定要用ll类型,不然会WA
    while(scanf("%d",&n) && n)
    {
        sum=0;
        for(i=2 ; i<=n ; i++)
            sum+=phi[i];
        printf("%lld\n",sum);
    }
    return 0;
}


POJ2478 ---- Farey Sequence

Time Limit: 1000MS Memory Limit: 65536K

Description
The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}

You task is to calculate the number of terms in the Farey sequence Fn.

Input
There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are no blank lines between cases. A line with a single 0 terminates the input.

Output
For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn.

Sample Input

2
3
4
5
0

Sample Output

1
3
5
9

本题求 F ( n ) F(n) F(n),实际上 F ( n ) F(n) F(n)就是欧拉函数从 φ ( 2 ) φ(2) φ(2)加到 φ ( n ) φ(n) φ(n)

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
typedef long long ll;
ll phi[1000005];

void euler_table(int n)
{
    int i,j;
    memset(phi,0,sizeof(phi));
    for (i=1;i<=n;i++)
        phi[i]=i;
    for (i=2;i<=n;i++)
    {
        if (phi[i]==i)
        {
            for (j=i;j<=n;j+=i)
            {
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}

int main()
{
    int n;
    //打表
    euler_table(1000000);
    int i;
    ll sum; //一定要用ll类型,不然会WA
    while(scanf("%d",&n) && n)
    {
        sum=0;
        for(i=2 ; i<=n ; i++)
            sum+=phi[i];
        printf("%lld\n",sum);
    }
    return 0;
}


## POJ3090 ---- Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K

Description

A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (x, y) with 0 ≤ x, y ≤ 5 with lines from the origin to the visible points.
在这里插入图片描述

Write a program which, given a value for the size, N, computes the number of visible points (x, y) with 0 ≤ x, y ≤ N.

Input

The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

Output

For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

Sample Input

4
2
4
5
231

Sample Output

1 2 5
2 4 13
3 5 21
4 231 32549

本题为POJ2478的变式,设欧拉函数从 φ ( 2 ) φ(2) φ(2)加到 φ ( n ) φ(n) φ(n)的和为sum,visible points的总个数就等于 s u m ∗ 2 + 3 sum*2+3 sum2+3 (+3是加上了(1,0), (0,1), (1,1) 三个点)

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int phi[1005];

void euler(int n)
{
    int i,j;
    for(i=1 ; i<=n ; i++)
        phi[i]=i;
    for(i=2 ; i<=n ; i++)
    {
        if(phi[i]==i)
        {
            for(j=i ; j<=n ; j+=i)
            {
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}


int main()
{
    euler(1005);
    int c,n;
    scanf("%d",&c);
    int i,cnt=0;
    ll sum;
    while(c--)
    {
        sum=0;
        scanf("%d",&n);
        cnt++;
        if(n>1)
            for(i=2 ; i<=n ; i++)
                sum+=phi[i];
        printf("%d %d %lld\n",cnt,n,sum*2+3);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值