Link:
Http://codeforces.com/problemset/problem/233/ B
Question:
B. Non-square equationtime limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard Input
Output
Standard output
Let's consider equation:
X
2 cores + CoresS(X)·XAccept-Encoding-NBytes = bytes 0, bytes,
WhereX, Bytes,NAre positive integers,S(X) Is
The function, equal to the sum of digits of numberXIn the decimal number system.
You are given an integerN, Find the smallest positive integer root of EquationX,
Or else determine that there are no such roots.
Input
A single line contains integerN(1 digit ≤ DigitNLimit ≤ limit 1018 )-
The equation parameter.
Please, do not use the % LLD specifier to read or write 64-bit integers in bytes ++. It is preferred to use Cin, cout streams
Or the % i64dspecifier.
Output
Print-1, if the equation doesn't have integer positive roots. Otherwise print such smallest integerX(XLatency> limit 0 ),
That the equation given in the statement holds.
Sample test (s) Input
2
Output
1
Input
110
Output
10
Input
4
Output
-1
Note
In the first test caseXMinute = minute 1 is the minimum root.S(1) between = between 1 and 12 cores + cores 1 · 1 Cores-cores 2 cores = cores 0.
In the second test caseXQuota = quota 10 is the minimum root.S(10) rows = Second 1 rows + second 0 rows = Second 1 and 102 rows + rows 1 · 10 rows-limit 110 rows = Limit 0.
In the third test case the equation has no roots.
Analysis and Summary:
I used to do a few math problems before, so I wanted to do it by using the binary method as soon as I saw it, but wa was always in Test 5. Later I was reminded that the formula could be changed to be instantly clear.
Set the formulaX2 cores + CoresS(X)·XAccept-Encoding-NLimit = Limit 0,
Deformation:
S (x) = N/X-X.
The approximate range of S (X) is 1 ~ 100, then enumerate the values of S (x), according to the values of S (X) and the equation S (X)
= N/X-X, returns x = SQRT (S (x) ^ 2/4 + n ).
Then, replace X with the original formula.X2 cores + CoresS(X)·XAccept-Encoding-NLimit = Limit 0
Check whether it meets the requirements.
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;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)