今日,学习了初等数论。做了几道比较简单的入门题(说来惭愧,本有9道,只做了4道)
0:hdu--1066、3240(卡特兰数)、1370(中国剩余定理)、3579、1573
Romantic
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Problem Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei
![](https://i-blog.csdnimg.cn/blog_migrate/50d0d5b948acfeebc45f5f1ccb8eaa48.jpeg)
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
Each case two nonnegative integer a,b (0<a, b<=2^31)
Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead.
Sample Input
77 51 10 44 34 79
Sample Output
2 -3 sorry 7 -3
Author
分析:题目要求二元一次方程的解,可用扩展欧几里得算法来实现。而当a,b的gcd不为1时,不存在;对于x为负数可用通解公式将其化为正数,而对于大于0的取最小亦可以用通解来控制。在这里因为最底层返回的x=1,y=0觉得可以保证最小(未证明,在最底层b为0,因而y应该可以取任意值,在通解范围内),不想竟真的过了。way:
#include <iostream>
using namespace std;
long x,y;
long extend_gcd(long a, long b){
long t,m;
if (b==0){
x=1;y=0;
return a;
}
m=extend_gcd(b, a%b);
t=x;x=y;
y=t-(a/b)*y;
return m;
}
int main()
{
long long a,b;
int ans;
while(cin>>a>>b){
ans=extend_gcd(a,b);
if(ans!=1)
cout<<"sorry"<<endl;
else {
if(x<0) {x+=b;y-=a;}
cout<<x<<" "<<y<<endl;
}
}
return 0;
A/B
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2 1000 53 87 123456789
Sample Output
7922 6060