hdu 3270 The Diophantine Equation
来源:互联网 发布:马自达3 知乎 编辑:程序博客网 时间:2024/05/21 10:11
The Diophantine Equation
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 933 Accepted Submission(s): 250
Problem Description
We will consider a linear Diaphonic equation here and you are to find out whether the equation is solvable in non-negative integers or not.
Easy, is not it?
Easy, is not it?
Input
There will be multiple cases. Each case will consist of a single line expressing the Diophantine equation we want to solve. The equation will be in the form ax + by = c. Here a and b are two positive integers expressing the co-efficient of two variables x and y.
There are spaces between:
1. “ax” and ‘+’
2. ‘+’ and “by”
3. “by” and ‘=’
4. ‘=’ and “c”
c is another integer that express the result of ax + by. -1000000<c<1000000. All other integers are positive and less than 100000. Note that, if a=1 then ‘ax’ will be represented as ‘x’ and same for by.
There are spaces between:
1. “ax” and ‘+’
2. ‘+’ and “by”
3. “by” and ‘=’
4. ‘=’ and “c”
c is another integer that express the result of ax + by. -1000000<c<1000000. All other integers are positive and less than 100000. Note that, if a=1 then ‘ax’ will be represented as ‘x’ and same for by.
Output
You should output a single line containing either the word “Yes.” or “No.” for each input cases resulting either the equation is solvable in non-negative integers or not. An equation is solvable in non-negative integers if for non-negative integer value of x and y the equation is true. There should be a blank line after each test cases. Please have a look at the sample input-output for further clarification.
Sample Input
2x + 3y = 1015x + 35y = 67x + y = 0
Sample Output
Yes.No.Yes.HINT: The first equation is true for x = 2, y = 2. So, we get, 2*2 + 3*2=10.Therefore, the output should be “Yes.”
#include <stdio.h>#include <string.h>long long gcd(long long a,long long b,long long &x,long long &y){ if(b == 0) { y = 0; x = 1; return a; } long long d = gcd (b,a%b,x,y); long long t=x; x = y; y = t - a/b*y; return d;}long long to_int(char *s){ if(s[1] == 0) return 1LL; long long res=0; for(int i = 0;s[i+1];++i) res = res*10 + (s[i]-'0'); return res;}int main(){ long long a[3]; char str[5][100]; while(scanf("%s",str[0]) != EOF) { int i; for(i=1;i < 5;++i) scanf("%s",str[i]); for(i=0;i<2;++i) a[i] = to_int(str[i<<1]); a[2]=0; for (i=0;str[4][i];++i) a[2] = a[2]*10 + str[4][i]-'0'; long long x=0,y=0; long long d=gcd(a[0],a[1],x,y); if(a[2] % d != 0) { printf("No.\n\n"); continue; } x = x * a[2]/d; long long t = a[1] / d; x = (x % t + t) % t; if((a[2] - x * a[0])/a[1] >= 0) { printf("Yes.\n\n"); continue; } y = y * a[2] / d; t = a[0] / d; y = ((y % t + t) % t); if((a[2] - y * a[1])/ a[0] >= 0 ) { printf("Yes.\n\n"); continue; } printf("No.\n\n"); } return 0;}/**///判断一个二元一次方程是否有非负整数解,///也就是给定ax+by=c,求是否存在x,y>=0使得式子成立。///先求出x的最小非负整数解和y的最小非负整数解,///暴力枚举也可///然后判断另一个数是否也非负即可///quetion? if C is negative integer?????int main(){ long long a,b,c,x,y,d,t,k; char str[100]; while(scanf("%s",str) != EOF) { int i; a = 0; for(i = 0;str[i+1]; ++i) a = a * 10 + str[i] - '0'; if(a == 0) a = 1; scanf("%s",str); scanf("%s",str); b=0; for(i = 0; str[i+1] ; ++i) b = b * 10 +str[i] - '0'; if(b == 0) b = 1; scanf("%s",str); scanf("%s",str); c=0; for(i=0;str[i];++i) c = c * 10 + str[i] - '0'; d = gcd(a,b,x,y); if( c%d !=0) { printf("No.\n\n"); continue; } x = x * c / d; t = b / d; x = (x % t + t)% t; k = (c - x * a) / b; if(k >=0) { printf("Yes.\n\n"); continue; } y = y * c / d; t = a / d; y = (y % t + t) % t; k = (c - y * b ) / a; if(k >=0 ) { printf("Yes.\n\n"); continue; } printf("No.\n\n"); } return 0;}*//*2x + 3y = 1015x + 35y = 67x + y = 0*/
- hdu 3270 The Diophantine Equation
- The Diophantine Equation hdu 3270
- HDU 3270 The Diophantine Equation
- HUDJ 3270 The Diophantine Equation
- 杭电 3270 The Diophantine Equation
- hdu3270 The Diophantine Equation
- EularProject 66:Diophantine equation
- projecteuler No.66 Diophantine equation
- Project Euler:Problem 66 Diophantine equation
- PE 66 Diophantine equation(Pell方程)
- The equation
- hdu 2199~Can you solve the equation?~二分法求解
- sgu 106 The equation
- SGU 106 The equation
- The equation----扩展欧几里得
- [SGU]106. The Equation
- sgu 106 The equation
- UVa10326 - The Polynomial Equation
- hdu 3275 lignt 树状数组
- MIT本科CS课程安排(2012版)
- css FF与IE区别
- android 图片旋转
- CSS 兼容大全
- hdu 3270 The Diophantine Equation
- 第十二周实验指导--任务四--类的组合与继承
- 防CSS代码挂马
- 归并排序
- 缓存类
- tiny 210 编译出错解决/init: line 103: can't open /r/dev/console: no such file
- 华为南研2011年“Openday”
- android GPS定位系统
- ssh配置文件