• CodeForces 233B


    题意

    对于公式 x2+s(x)·xn=0, 给出n(1 ≤ n ≤ 1018), 求是否存在x使得该式成立,其中s(x)代表数字x的数位之和。(如s(102) = 1 + 0 + 2 = 3)

    思路

    思路+暴力
    为使公式成立,x可以取到 999999999 (再大就没必要枚举了),s(x) max= 81,所以只需要枚举s(x) (1~81),通过s(x) 和 n 和公式 x=i+i2+4n2 解一元二次方程求出x,再求x的数位之和,判断式子是否成立即可。若成立则跳出循环。

    AC代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    
    using namespace std;
    typedef long long ll;
    
    ll S(ll x){
        ll res = 0;
        while(x){
            res += x % 10;
            x /= 10;
        }
        return res;
    }
    int main()
    {
        ll n;
        scanf("%lld",&n);
        ll ans = -1;
        for(ll i = 1; i <= 81; i++ ){
            ll x = (sqrt(i*i+4*n)-i)/2;
            ll sx = S(x);
            if( x*x + sx*x == n ){
                ans = x;
                break;
            }
        }
        printf("%lld
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    2017/3/27 morning
    2017/3/24 afternoon
    2017/3/24 morning
    2017/3/21 afternoon
    2017/3/21 morning
    2017/3/20 afternoon
    2017/3/20 morning
    2017/3/16 afternoon
    2017/3/16 morning
    2017/3/15afternoon
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740557.html
Copyright © 2020-2023  润新知