Topic related
Topic link
AtCoder Regular Contest 108 A 题,https://atcoder.jp/contests/arc108/tasks/arc108_a。
Problem Statement
Given are integers S and P. Is there a pair of positive integers (N,M) such that N+M=S and N×M=P?
Input
Input is given from Standard Input in the following format:
S P
Output
If there is a pair of positive integers (N,M)(N,M) such that N+M=SN+M=S and N×M=PN×M=P, print Yes
; otherwise, print No
.
Samples1
Sample Input 1
3 2
Sample Output 1
Yes
Explaination
- For example, we have N+M=3 and N×M=2 for N=1,M=2.
Samples2
Sample Input 2
1000000000000 1
Sample Output 2
No
Constraints
- All values in input are integers.
Problem solving report
Topic translation
Give two positive integers S and P, and ask whether there are positive integers N and M that satisfy N+M=S and N*M=P.
Topic analysis
This question is a relatively simple math question. According to the requirements of the question, we know S and P, and ask whether there are N and M. In this way, we can write two equations.
, This is a two-variable linear equation system, only need to solve this equation system. Using the substitution method, we zero M=SN, and then we get it after bringing in the equation . Using the left-hand rule, we get a quadratic equation in one variable,, so that the root of the equation can be solved by the Veda theorem.
According to the Veda theorem, we know that , namely . According to the root formula of a quadratic equation in one variable, we know:
- , The equation has two imaginary numbers. This question does not meet the requirements.
- , The equation has two equal integers. May meet the requirements of this question.
- , The equation has two unequal integers. May meet the requirements of this question.
According to the root formula of the quadratic equation in one variable,, according to the data range provided by the title, we know that b=-S, and S>0, so b>0. So when equal to zero, the equation has two equal positive integers. When the question requires two positive integers, it must be satisfied , that is , there can be two positive integers.
Data range estimation
According to the data range provided by the title , we need to use the square in the calculation, that is, the largest data is , which means that it exceeds the range of unsigned long long. What should I do? Just change to a larger data type. The easiest way is to treat all data as doubles, so that the problem can be solved.
to sum up
The difficulty of this question is not great, but there are more details.
AC reference code
//https://atcoder.jp/contests/arc108/tasks/arc108_a
//A - Sum and Product
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double s,p;
cin>>s>>p;
//n+m=s m=s-n
//n*m=p n*(s-n)=p -n^2+s*n-p=0; n^2-s*n+p=0
//需要有两个正整数跟,根据韦达定理, b^2-4ac,也就是
//s*s-4*1*p=s*s+4*p>0
double delta=s*s-4*p;
if (delta<0) {
//虚数根
cout<<"No\n";
} else if (0==delta) {
//相同根
cout<<"Yes\n";
} else {
//计算出根
double x=(s+sqrt(delta))/2;
double y=(s-sqrt(delta))/2;
if (y>0 && x+y==s && x*y==p) {
cout<<"Yes\n";
} else {
cout<<"No\n";
}
}
return 0;
}
When writing the code, I used a rather stupid method to directly calculate the two and compare them.
time complexity
O (1)。
Space complexity
O (1)。
enumerate
First of all thanks to T_a_r_j_a_n for his comment. This question can indeed change the way of thinking, which is to enumerate from 1 to sqrt(P), whether there is data that meets the conditions. Why do we need to open the root sign, because we have one product, N*M=P.
AC reference code
//https://atcoder.jp/contests/arc108/tasks/arc108_a
//A - Sum and Product
//从 1 到 sqrt(P) 中枚举是否存在 N 和 M。
#include <bits/stdc++.h>
using namespace std;
//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL
int main() {
#ifndef __LOCAL
//这部分代码需要提交到OJ,本地调试不使用
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#endif
long long s,p;
cin>>s>>p;
long long t=sqrt(p);
for (long long i=1; i<=t; i++) {
long long n=i;
long long m=s-i;
if (n*m==p) {
cout<<"Yes\n";
return 0;
}
}
cout<<"No\n";
#ifdef __LOCAL
//这部分代码不需要提交到OJ,本地调试使用
system("pause");
#endif
return 0;
}
time complexity
, In fact, it is .
Space complexity
O (1)。