版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/83990899
整数解
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 44274 Accepted Submission(s): 15438
Problem Description
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
x + y = 9,x * y = 15 ? 找不到这样的整数x和y
1+4=5,1*4=4,所以,加起来等于5,乘起来等于4的二个整数为1和4
7+(-8)=-1,7*(-8)=-56,所以,加起来等于-1,乘起来等于-56的二个整数为7和-8
Input
输入数据为成对出现的整数n,m(-10000<n,m<10000),它们分别表示整数的和与积,如果两者都为0,则输入结束。
Output
只需要对于每个n和m,输出“Yes”或者“No”,明确有还是没有这种整数就行了。
Sample Input
9 15
5 4
1 -56
0 0
Sample Output
No Yes Yes
1.x + y = n, x * y = m 所以 x ^ 2 - n *x + m = 0,判断方程组是否有整数解即可
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
if(n == 0 && m == 0) break;
if( sqrt(n * n - 4 * m) == (int)sqrt(n * n - 4 * m)) printf("Yes\n");
//sqrt函数计算负值返回nan,(int)会把nan转换为-2147483648
else printf("No\n");
}
return 0;
}
2.题目数据范围不大,m最大为10000,所以只要在(-100, 100)里面找解就可以了
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 998244353
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
if(n == 0 && m == 0) break;
bool flag = false;
for(int i = -100; i <= 100; i++){
if(i * (n - i) == m){
flag = true;
break;
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}