odeforces Round #144 (Div. 2) B

http://codeforces.com/contest/233/problem/B

s(x)最大为81.所以x*(x+s(x))==n假如有x存在,则x必然小于sqrt(n)。且大于sqrt(n)-82。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 
 7 using namespace std;
 8 
 9 long long n,ans;
10 
11 int getsum(long long x)
12 {
13     int s=0;
14     while(x)
15     {
16         s+=x%10;
17         x/=10;
18     }
19     return s;
20 }
21 
22 int main()
23 {
24     while(~scanf("%I64d",&n))
25     {
26         ans=-1;
27         long long tmp=(long long ) sqrt(n);
28         long long i=(tmp-82>0?tmp-82:1);
29         for(;i<=tmp;i++)
30             if(i*i+i*getsum(i)==n)
31             {
32                 ans=i;
33                 break;
34             }
35         printf("%I64d\n",ans);
36     }
37     return 0;
38 }
posted @ 2012-10-12 19:06  Missa  阅读(203)  评论(0编辑  收藏  举报