数论专题训练2016.3.21

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110042#overview  密码:nefu

一些题目相对简单了些:

A题POJ2478: http://poj.org/problem?id=2478

题面描述:


Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14329   Accepted: 5668

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 <= 10 6). 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

代码实现:

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int euler[1000010];
void getEuler()
{
    memset(euler,0,sizeof(euler));
    euler[1]=1;
    for(int i=2;i<=1000000;i++)
    {
        if(!euler[i])
        for(int j=i;j<=1000000;j+=i)
        {
            if(!euler[j])
            euler[j]=j;
            euler[j]=euler[j]/i*(i-1);
        }
    }
}
long long f[1000010];
void fi()
{
    memset(f,0,sizeof(f));
    for(int i=2;i<=1000000;i++)
    {
        f[i]=f[i-1]+euler[i];
    }
}
int main()
{
    int n;
    getEuler();
    fi();
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        break;
        printf("%lld\n",f[n]);
    }
    return 0;
}

B题:POJ 1006  http://poj.org/problem?id=1006

题面描述:

Biorhythms
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 125974   Accepted: 39821

Description

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>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int M=29;

int main()
{
    int p,e,i,d;
    int sum;
    int casenum=0;
    while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF)
    {
        if(p==-1&&e==-1&&i==-1&&d==-1)
        break;
        sum=(5544*p+14421*e+1288*i-d+21252)%21252;
        if(sum==0)
        sum=21252;
        printf("Case %d: the next triple peak occurs in %d days.\n",++casenum,sum);
    }
    return 0;
}

C题:codeforces 7C : http://www.codeforces.com/problemset/problem/7/C

题面描述:

C. Line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist.

Input

The first line contains three integers AB and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.

Output

If the required point exists, output its coordinates, otherwise output -1.

Examples
input
2 5 3
output
6 -3

代码实现:

#include <stdio.h>
#include <iostream>

using namespace std;


long long gcd(long long a,long long b)
{
    if(b==0)
    return a;
    else return gcd(b,a%b);
}

void ex_gcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    ex_gcd(b,a%b,y,x);
    y-=a/b*x;
    return;
}

int main()
{
    long long a,b,c;
    long long x0,y0;
    while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF)
    {
        c=-c;
        if(a==0&&b!=0)
        {
            if(c%b==0)
            {
                printf("0 %lld\n",c/b);
            }
            else printf("-1\n");
        }
        if(a!=0&&b==0)
        {
            if(c%a==0)
            {
                printf("%lld 0\n",c/a);
            }
            else printf("-1\n");
        }
        if(a!=0&&b!=0)
        {
            if(c==0)
            {
                printf("0 0\n");
            }
            else
            {
                long long d=gcd(a,b);
                if(c%d!=0)
                {
                    printf("-1\n");
                }
                else
                {
                    a=a/d;
                    b=b/d;
                    c=c/d;
                    ex_gcd(a,b,x0,y0);
                    x0=x0*c;
                    x0=((x0%b)+b)%b;
                    y0=(c-a*x0)/b;
                    printf("%lld %lld\n",x0,y0);
                }
            }
        }
    }
    return 0;
}

D题:HDU2588 : http://acm.hdu.edu.cn/showproblem.php?pid=2588 

题面描述:

GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1442    Accepted Submission(s): 676


Problem Description
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.
 

Input
The 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.
 

Output
For each test case,output the answer on a single line.
 

Sample Input
   
   
   
   
3 1 1 10 2 10000 72
 

Sample Output
   
   
   
   
1 6 260


代码实现:

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int euler;
long long getEuler(long long n)
{
    long long ans=n;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            ans-=ans/i;
            while(n%i==0)
            n/=i;
        }
    }
    if(n>1)
    ans-=ans/n;
    return ans;
}

int main()
{
    int T;
    int n,m;
    int sum;
    scanf("%d",&T);
    while(T--)
    {
        sum=0;
        scanf("%d%d",&n,&m);

        for(int i=1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                if(i>=m)
                {
                    sum+=getEuler(n/i);
                }
                if((n/i)!=i&&(n/i)>=m)
                {
                    sum+=getEuler(i);
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}


你可能感兴趣的:(数论专题训练2016.3.21)