码迷,mamicode.com
首页 > 其他好文 > 详细

中国剩余定理&欧拉函数

时间:2017-04-26 14:46:52      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:mil   rac   find   测试数据   repr   except   origin   euc   example   

A - 欧拉函数1

HDU - 1286                    

新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的公约数,否则都是新朋友,现在会长想知道究竟有几个新朋友?请你编程序帮会长计算出来。Input第一行是测试数据的组数CN(Case number,1<CN<10000),接着有CN行正整数N(1<n<32768),表示会员人数。Output对于每一个N,输出一行新朋友的人数,这样共有CN行输出。
Sample Input

2
25608
24027

Sample Output

7680
16016
#include"stdio.h"
typedef long long ll;
int main()
{
    ll i,j,phi[100000],n,m;
    for(i=2;i<100000;i++)
    phi[i]=0;phi[1]=1;
    for(i=2;i<100000;i++)
    {
        if(phi[i]==0)
        {
            for(j=i;j<100000;j+=i)
            {
                if(phi[j]==0)
                phi[j]=j;
                phi[j]=phi[j]*(i-1)/i;
            }
        }
    }
    scanf("%lld",&n);i=0;
    while(i++<n)
    {
        scanf("%lld",&m);
        printf("%lld\n",phi[m]);
    }
}

B - 欧拉函数2

HDU - 1787 

Do you have spent some time to think and try to solve those unsolved problem after one ACM contest?
No? Oh, you must do this when you want to become a "Big Cattle".
Now you will find that this problem is so familiar:
The greatest common divisor GCD (a, b) of two positive integers a and b, sometimes written (a, b), is the largest divisor common to a and b. For example, (1, 2) =1, (12, 18) =6. (a, b) can be easily found by the Euclidean algorithm. Now I am considering a little more difficult problem:
Given an integer N, please count the number of the integers M (0<M<N) which satisfies (N,M)>1.
This is a simple version of problem “GCD” which you have done in a contest recently,so I name this problem “GCD Again”.If you cannot solve it still,please take a good think about your method of study.
Good Luck!
InputInput contains multiple test cases. Each test case contains an integers N (1<N<100000000). A test case containing 0 terminates the input and this test case is not to be processed.
OutputFor each integers N you should output the number of integers M in one line, and with one line of output for each line in input.
Sample Input

2
4
0

Sample Output

0
1
#include"stdio.h"
typedef long long ll;
int main()
{
    ll i,j,n,ret,m;
    while(scanf("%lld",&n)!=EOF)
    {
        if(n==0)break;
        if(n==1)
        {
            printf("0\n");continue;
        }
        ret=n;m=n-1;
        for(i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                ret=ret-ret/i;
                n=n/i;
                while(n%i==0)
                n=n/i;
            }
        }
        if(n>1)
        ret=ret-ret/n;
        printf("%lld\n",m-ret);
    }

C - 欧拉函数3

HDU - 3501                    

Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1.InputFor each test case, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.OutputFor each test case, you should print the sum module 1000000007 in a line.Sample Input

3
4
0

Sample Output

0
2
#include"stdio.h"
typedef long long ll;
int main()
{
    ll i,j,n,ret,m;
    while(scanf("%lld",&n)!=EOF)
    {
        if(n==0)break; 
        ret=n;m=n;
        for(i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                ret-=ret/i;
                n=n/i;
                while(n%i==0)
                n=n/i;
            }
        }
        if(n>1)
        ret=ret-ret/n;
        printf("%lld\n",((m-1)*m/2-ret*m/2)%1000000007);
    }
}

D - 欧拉函数4

HDU - 2824                    

The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)InputThere are several test cases. Each line has two integers a, b (2<a<b<3000000).OutputOutput the result of (a)+ (a+1)+....+ (b)Sample Input

3 100

Sample Output

3042
#include"stdio.h"
typedef long long ll;
int main()
{
    ll i,j,n1,n2,phi[3000001]={0};
    for(i=2;i<3000001;i++)
    phi[i]=0;
    for(i=2;i<3000001;i++)
    {
        if(phi[i]==0)
        {
            for(j=i;j<3000001;j+=i)
            {
                if(phi[j]==0)
                phi[j]=j;
                phi[j]=phi[j]*(i-1)/i;
            }
        }
        phi[i]=phi[i-1]+phi[i];
    }
    while(scanf("%lld%lld",&n1,&n2)!=EOF)
    {
        printf("%lld\n",phi[n2]-phi[n1-1]);
    }
}

E - 欧拉函数5

HDU - 2588                    

The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.InputThe first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.OutputFor each test case,output the answer on a single line.Sample Input

3
1 1
10 2
10000 72

Sample Output

1
6
260

中国剩余定理&欧拉函数

标签:mil   rac   find   测试数据   repr   except   origin   euc   example   

原文地址:http://www.cnblogs.com/lch316/p/6768151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!