CodeForces-233B-Non-square Equation

Description

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 an integer n, find the smallest positive integer root of equation x, or else determine that there are no such roots.

Input

A single line contains integer n(1?≤?n?≤?1018) — the equation parameter.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Output

Print -1, if the equation doesn’t have integer positive roots. Otherwise print such smallest integer x(x?>?0), that the equation given in the statement holds.

Sample Input

Input

2

Output

1

Input

110

Output

10

Input

4

Output

-1

Hint

In the first test case 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(10)?=?1?+?0?=?1 and 102?+?1·10?-?110?=?0.

In the third test case the equation has no roots.

解题思路:

原方程变形后:s(x)=n/x-x;可以推测s(x)的范围为1-100.然后利用s(x)求解x。最后将s(x)和x带入原方程。若成立,则x为所求。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

typedef long long LL;

LL digitSum(LL n)
{
    LL sum = 0;
    while(n)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}

LL n;

int main()
{
    while(cin >> n)
    {

        LL ds,ans=-1,x,tmp;

        for ( int i = 1; i <= 100; ++i)
        {
            tmp = i*i/4+n;
            x= -i/2+sqrt(tmp);
            ds=digitSum(x);
            if (x*x+ds*x-n==0){
                ans=x;
                break;
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}
时间: 2024-10-05 06:12:01

CodeForces-233B-Non-square Equation的相关文章

Codeforces 828B Black Square(简单题)

Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n?×?m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum pos

CodeForces 1A Theatre Square

A - Theatre Square Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 1A Description Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the

(dp)CodeForces - 300D Painting Square

Vasily the bear has got a large square white table of n rows and n columns. The table has got a black border around this table.  The example of the initial table at n = 5. Vasily the bear wants to paint his square table in exactly k moves. Each move

Codeforces 919 E Congruence Equation

题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1<=n<=x ) satisfy  where a,b,pa,b,p are all known constants. 输入输出格式 输入格式: The only line contains four integers a,b,p,xa,b,p,x ( 2<=p<=10^{6}+32<

codeforces 1187F. Expected Square Beauty

求$E({B(x)}^2)$,考虑$B(x)$为每一位与前一位不同的期望次数 令$A(x)$表示第$x$位与第$x-1$位不同的概率,特别地,$A(1)=1$ $$E({B(x)}^2)=E({(\sum_{i=1}^n A(i))}^2)$$ 把式子展开得, $$E({B(x)}^2)=\sum_{i=1}^n \sum_{j=1}^n E(A(i)\times A(j))$$ 显然如果$|i-j|>1$,$A(i)$与$A(j)$是独立的,$E(A(i)\times A(j))=E(A(i)

CodeForces Beta Round #1

Codeforces Beta Round #1 A. Theatre Square [题意]一个n*m的矩形广场,用a*a的方形石板铺设,问最少需要多少块石板能铺满广场. [思路]水题,从n方向来看能能够铺设ceil(n/a)块,从m方向来看能能够铺设ceil(m/a)块,总共有ceil(n/a)*ceil(m/a)块. 1 /* 2 ** CodeForces 1A Theatre Square 3 ** Created by Rayn @@ 2014/05/18 4 */ 5 #inclu

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas

Codeforces Round #249 (Div. 2) A. Black Square

水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector<int> a(4); cin >> a[0] >> a[1]>>a[2]>>a[3]; string str; cin >> str; int res = 0; for(int i = 0 ; i

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

CodeForces 20B Equation 水题

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define l