Link:
Http://codeforces.com/problemset/problem/233/B
Topic:
B. Non-square equation
Time limit per test
1 second
Memory limit per test
256 Megabytes
Input
Standard input
Output
Standard output
Let ' s consider equation:
X2+s (x) x-n=0,
Where x,n are positive integers, s (x) is the function, equal to the sum of digits of number x in the decimal number system .
You are given a integer n, find the smallest positive integer root of equation x, or else determine that there are no suc H roots.
Input
A single line contains integer n (1≤n≤1018)-the equation parameter.
Please, don't use the%lld specifier to read or write 64-bit integers inс++. It is preferred to use CIN, cout streams or the%i64dspecifier.
Output
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Print-1, if the equation doesn ' t have integer positive roots. Otherwise print such smallest integer x (x>0), which is equation given in the statement.
Sample Test (s)
Input
2
Output
1
Input
110
Output
10
Input
4
Output
-1
Note
In the ' The ' the ' x=1 is the ' minimum root. As S (1) =1 and 12+1 1-2=0.
In the second Test case x=10 is the minimum root. As S (=1+0=1) and 102+1 10-110=0.
In the third test case the equation has no roots.
Analysis and Summary:
Before the math problem, so the first to see the use of the two-way, but has been WA in Test 5, and then the reminder can be distorted formula, instantaneous clarity.
x-n=0 The formula X2+s (x) to deform:
S (x) = n/x-X.
You can roughly estimate the range of s (x) between 1~100, and then enumerate the values of s (x), and then, according to the value of s (x) and the equation s (x) = n/x-X, solve the x = sqrt (s (x) ^2/4 + N).
The x is then x2+s to the original formula (x) x-n=0 to see if it fits.
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long Int64;
Int64 n, SX;
Int64 Digitsum (Int64 N) {
int64 sum=0;
while (n) {
sum = n%10;
n/=10;
}
return sum;
}
int main () {
while (CIN >> N) {
int64 x=1, End=1e8, ans=-1;
For (Int64 i=1 i<=100; ++i) {
Int64 tmp = i*i/4+n;
x = sqrt (tmp)-I/2;
SX = digitsum (x);
if (x*x+sx*x-n==0) {
ans=x;
break;
}
}
cout << ans << endl;
return 0;
}
Author: csdn Blog shuangde800