CodeForces 233B Non-square Equation

链接:

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

题目:

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 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 %I64dspecifier.
Output

更多精彩内容: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), 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 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.

分析与总结:

之前很少做数学题,所以一看到就想用二分法做,但是一直WA在test 5,后来经提醒可以将公式变形,瞬间明朗。

把公式x2+s(x)·x-n=0, 进行变形:

S(x) = n/x - x。

可大致估计S(x)的范围在1~100之间, 然后枚举S(x)的值,根据S(x)的值和方程S(x) = n/x - x,解出x = sqrt( S(x)^2/4 + n ).

然后把x代入原公式x2+s(x)·x-n=0 看是否符合。

代码:

#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;
}

作者:csdn博客 shuangde800

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, input
, integer
, test
, output
The
美的m3l233b好不好、美的m3l233b、xprinter xp233b、m3l233b、xp 233b,以便于您获取更多的相关知识。

时间: 2024-11-02 16:20:53

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

Matrix libraries for C and C++

Document options Print this page E-mail this page Congratulations! developerWorks wins Jolt Hall of Fame award Rate this page Help us improve this content Level: Introductory Andrew Blais (onlymice@attbi.com), Researcher and writer 01 Jul 2002 This a

EM square

看<windows程序设计>中的一些字体方面的信息,不是很明白,通过查阅得到了一些重要信息.   在字体排版中经常使用EM square,每一种TrueType字体都有一个EM square,而且只有TrueType的字体才有这个EM square.EM square其实就是在实际设计字体时用到的正方形,而这个正方形里面有很多个小方格.一般来说的话,一个EM square里面水平方向,垂直方向都有2048个小方格.我们实际使用的TrueType字体都是基于这个尺寸缩放的.   如果在程序中打印

MathType公式后面的Equation怎么隐藏

  MathType公式后面的Equation怎么隐藏 MathType公式后面的equation示例 解决方法操作步骤如下: 1.打开Word文件后,在Word菜单中选择"插入"--"引用"--"题注".(此操作是在Word 2003中,Word 2007及以后的版本直接在"引用"里面). 在Word菜单中选择"插入"--"引用"--"题注" 2.在弹出的"

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

Geeks 面试题之Maximum size square sub-matrix with all 1s

Maximum size square sub-matrix with all 1s Given a binary matrix, find out the maximum size square sub-matrix with all 1s. For example, consider the below binary matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 The maximum square s

Codeforces B. Taxi 算法题解

简单总结题意: 有一个数组,数组中的数值不会大于4,即只能是1,2,3,4,要把这些数值装进一个容量最大为4的容器里面,使得所用到这样的容器的个数最小. 经测试数据很大,会有10万个数据,所以这里我并不用排序数据,而是使用counting sort的思想,根据特定的数据优化,使得题解时间复杂度为O(n). 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 程序如下: #include <iostream>

CodeForces:200C: Football Championship

地址链接: CF:  http://codeforces.com/problemset/problem/200/C HUST Virtual Judge: http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=28923 题目: C. Football Championship time limit per test 2 seconds memory limit per test 256 megabytes input s

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta