中国剩余定理&欧拉函数

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

F - 扩展欧几里德1

POJ - 1061                   
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input
输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

Output
输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

Sample Input
1 2 3 4 5
Sample Output
4
 
#include"stdio.h"
long long int gcd(long long int a,long long int b)
{
    if(b==0)
    return a;
    return gcd(b,a%b);
}
long long int extgcd(long long int a,long long int b,long long int &X,long long int &Y)
{
    long long int d=a;
    if(b!=0)
    {
        d=extgcd(b,a%b,Y,X);
        Y=Y-a/b*X;
    }
    else
    {
        X=1;Y=0;
    }
    return d;
}

int main()
{
    long long int t,a,b,r,x,y,m,n,d,l,X=0,Y=0;
    while(scanf("%lld %lld %lld %lld %lld",&x,&y,&m,&n,&l)!=EOF)
    {
        a=n-m;b=l;d=gcd(a,b);
        if((x-y)%d==0)
        {
            a/=d;b/=d;
            extgcd(a,b,X,Y);
            t=(X*((x-y)/d))%b;
            while(t<0)
            {
                t+=b;
            }
            printf("%lld\n",t);
        }
        else
        printf("Impossible\n");
    }   
}

G - 扩展欧几里德2

POJ - 2115                    

A Compiler Mystery: We are given a C-language style for loop of type 
for (variable = A; variable != B; variable += C)

statement;

I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2 k) modulo 2 k.

Input
The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2 k) are the parameters of the loop.

The input is finished by a line containing four zeros.

Output
The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate.

Sample Input
3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0
Sample Output
0
2
32766
FOREVER

H - 中国剩余定理

POJ - 1006                 
Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
Input
You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output
For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:

Case 1: the next triple peak occurs in 1234 days.

Use the plural form ``days'' even if the answer is 1.

Sample Input
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
   
#include"stdio.h"
typedef long long ll;
ll ext_gcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;y=0;
        return 0;
    }
    else
    {
        ext_gcd(b,a%b,y,x);
        y=y-a/b*x;
        return 0;
    }
}
ll china(ll b[3],ll m[3],ll k)
{
    ll x=0,y=0,a=0,d,Mi,i,j,M=1;
    for(i=0;i<k;i++)
    {
        M=M*m[i];
    }
    for(i=0;i<k;i++)
    {
        Mi=M/m[i];
        d=ext_gcd(m[i],Mi,x,y);//y=y%printf("%lld*%lld+%lld*%lld=%lld\n",m[i],x,Mi,y,m[i]*x+Mi*y);
        a=(a+y*Mi*b[i])%M;
    }
    if(a>0)
    return a;
    else
    return a+M;
}
int main()
{
    ll b[3],m[3]={23,28,33},k,j,i,n=1;
    while(scanf("%lld%lld%lld%lld",&b[0],&b[1],&b[2],&k)!=EOF)
    {
        if(b[0]<0||b[1]<0||b[2]<0||k<0)break;
        b[0]=b[0]%23;b[1]=b[1]%28;b[2]=b[2]%33;
        j=china(b,m,3);j=j-k;
        if(j<0)
        {
            j=j+23*28*33;
        }
        printf("Case %lld: the next triple peak occurs in %lld days.\n",n,j);n++;
    } 
} 

I - 中国剩余定理(这个两两不互质)

POJ - 2891 

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

 

Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ ik) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ ik).

 

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

 

Sample Input
2
8 7
11 9
Sample Output
31
Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

 

转载于:https://www.cnblogs.com/lch316/p/6768151.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值