杭电 3270 The Diophantine Equation

http://acm.hdu.edu.cn/showproblem.php?pid=3270

The Diophantine Equation

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1146    Accepted Submission(s): 297

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?

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.

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 = 10
15x + 35y = 67
x + 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.”

一道很简单的题目,稍稍细心一些就能很快的AC,(我愚钝呀,弄了好久,后来想想觉得A出来之前大脑是短路状态,,这是为什么啊)需要注意的是输入,处理的话也不是很难。 

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int qiujie(int a,int b,int c)
{
    int x;
    for(x=0; x<=c/a; x++) //枚举x可取的所有可能
    {
        if((c-a*x)%b==0)  //判断是否存在正整数y
        {
            return 1;
        }
    }
    return 0;
}
int main()
{
    int a,b,c,i,j;
    char s1[16],s2[16],ch,cj;
    while(cin>>s1>>ch>>s2>>cj>>c)
    {
        a=b=0;
        for(i=0; i<strlen(s1)-1; i++)
        {
            if(s1[i]!='x')
            {
                a+=s1[i]-'0';
                if(s1[i+1]!='x')
                    a*=10;
            }
            /* a*=10;
              a+=s1[i]-'0';*/

        }
        for(j=0; j<strlen(s2)-1; j++)
        {
            if(s2[j]!='y')
            {
                b+=s2[j]-'0';
                if(s2[j+1]!='y')
                    b*=10;
            }
            /*  b*=10;
              b+=s2[j]-'0';*/
        }
        if(s1[0]=='x')a=1;
        if(s2[0]=='y')b=1;
        if(qiujie(a,b,c)==1)
            printf("Yes.\n\n");
        else
            printf("No.\n\n");

    }
    return 0;
}

杭电 3270 The Diophantine Equation

时间: 2024-12-20 15:44:40

杭电 3270 The Diophantine Equation的相关文章

HUDJ 3270 The Diophantine Equation

The Diophantine Equation Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1133    Accepted Submission(s): 292 Problem Description We will consider a linear Diaphonic equation here and you are to

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电 HDU ACM 1496 Equations

Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6065    Accepted Submission(s): 2455 Problem Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a,

杭电 1002 A + B Problem II(大数处理)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 209179    Accepted Submission(s): 40226 Problem Description I have a very simple problem for you. Given two integers A and B, yo

大数A+B 【杭电-1002】 附题

/* A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 209912    Accepted Submission(s): 40404 Problem Description I have a very simple problem for you. Given two integers A and B,

2019 杭电多校 第五场

2019 Multi-University Training Contest 5 补题链接:2019 Multi-University Training Contest 5 罚时爆炸 自闭场 1004 equation (HDU 6627) 题意: 给定一个整数 \(C\) 和 \(N\) 组 \(a_i,b_i\),求 \(∑_{i=1}^N|a_i\cdot x + b_i| = C\) 的所有解,如果有无穷多个解就输出 -1. 思路 分类讨论 分类讨论去绝对值.根据 \(b_i / a_i

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num