数论练习


Language:
青蛙的约会
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 103143 Accepted: 20017

Description

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 
我们把这两只青蛙分别叫做青蛙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<iostream>
using namespace std;
void exp_gcd(long long& x,long long& y,long long& d,long long a,long long b)
{
	if(!b)
	{
		d=a;
		x=1;
		y=0;
		return ;
	}
	exp_gcd(y,x,d,b,a%b);
	y-=x*(a/b);

}
int main()
{
	long long x1,y1,m,n,l;
	while(cin>>x1>>y1>>m>>n>>l)
	{
		long long x,y,s,v,d;
		v=n-m;
		s=x1-y1;
		exp_gcd(x,y,d,v,l);
		if(s%d!=0)
		{
			cout<<"Impossible"<<endl;
			continue;
		}
		long long rate=s/d;		
		x*=rate;
		y*=rate;
		while(x<0||x-l>0)
		{
			if(x<0)x+=l;
			else x-=l;
		}
		cout<<x<<endl;

	}
	return 0;
}


分拆素数和

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31769    Accepted Submission(s): 13686


Problem Description
把一个偶数拆成两个不同素数的和,有几种拆法呢?
 

Input
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
 

Output
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
 

Sample Input
30 26 0
 

Sample Output
3 2


#include<iostream>
#include<cstring>
using namespace std;
bool vis[10000];
int main()
{
    memset(vis,false,sizeof(vis));
    for(int i=2;i<10000;++i)
        for(int j=i*2;j<=10000;j+=i)
            vis[j]=true;
    int x;
    while(cin>>x&&x!=0)
    {
        int t=x/2;
        int flag=0;
        for(int i=3;i<t;i++)
            if(vis[i]==false&&vis[x-i]==false)
            flag++;
        cout<<flag<<endl;

    }
    return 0;

}
Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 126661 Accepted: 40085

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<iostream>
const int mod=21252;
using namespace std;
void ext_gcd(int a,int b,int& x ,int& y)
{
    if(!b)
    {
        x=1;
        y=0;
        return ;
    }
    ext_gcd(b,a%b,y,x);
    y-=x*(a/b);
}
int gcd(int a,int b)
{
    return b==0 ? a : gcd(b,a%b);
}
int main()
{
    int rate1,rate2,rate3;

    rate1=mod/23;
    rate2=mod/28;
    rate3=mod/33;
    int x1,x2,x3;
    int y1,y2,y3;
    ext_gcd(rate1,23,x1,y1);
    ext_gcd(rate2,28,x2,y2);
    ext_gcd(rate3,33,x3,y3);
    x1=(x1%23+23)%23;
    x2=(x2%28+28)%28;
    x3=(x3%33+33)%33;
    int p,e,i,d;
    int kl=0;
    while(cin>>p>>e>>i>>d)
    {
        if(p==-1&&e==-1&&i==-1&&d==-1)
            break;
        int k1=x1*rate1;
        int k2=x2*rate2;
        int k3=x3*rate3;
        long long  ans=(k1*p%mod+k2*e%mod+k3*i%mod+mod)%mod;
        ans=(ans-d+mod)%mod;
        if(ans==0)ans=mod;
        kl++;
        cout<<"Case "<<kl<<": the next triple peak occurs in "<<ans<<" days."<<endl;
    }
    return 0;
}

Raising Modulo Numbers
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6190 Accepted: 3635

Description

People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow: 

Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions AiBi from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers. 

You should write a program that calculates the result and is able to find out who won the game. 

Input

The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 <= M <= 45000). The sum will be divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.

Output

For each assingnement there is the only one line of output. On this line, there is a number, the result of expression 

(A1B1+A2B2+ ... +AHBH)mod M.

Sample Input

3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13

#include<iostream>
using namespace std;
long long power(long long  a,long long   n,long long  m)
{
    if(n==0)return 1;
    int x=power(a,n/2,m);
    long long ans = (long long )x*x%m;
    if(n%2==1)ans=ans*a%m;
    return (int)ans;
}
struct node
{
    int a;int b;
}num[45005];
int main()
{
    int  n;
    while(cin>>n)
    {
        while(n--)
        {
            long long   t;
            cin>>t;
            int nnum;
            cin>>nnum;
            long long sum=0;
            for(int i=0;i<nnum;++i)
            {
                cin>>num[i].a>>num[i].b;
                sum=(sum%t+power(num[i].a,num[i].b,t)%t)%t;
            }
            cout<<sum<<endl;

        }
    }
}

Lowest Common Multiple Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49897    Accepted Submission(s): 20623


Problem Description
求n个数的最小公倍数。
 

Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
 

Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
 

Sample Input
2 4 6 3 2 5 7
 

Sample Output
12 70

#include<iostream>
#include<cstring>
using namespace std;
long long  gcd(long long  a,long long  b)
{
    return b==0?a:gcd(b,a%b);
}
bool vis[10000];
int main()
{
    int n;
    int num[100];
    int  t;
    while(cin>>n)
    {
        for(int i=0;i<n;++i)
            cin>>num[i];
        long long s=num[0]*num[1]/gcd(num[0],num[1]);
        for(int i=2;i<n;++i)
        {
            t=gcd(s,num[i]);
            s=s*num[i]/t;

        }
        cout<<s<<endl;
    }
    return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值