题意
给你一个一元二次方程x2 + s(x)⋅x − n = 0x2 + s(x)·x − n = 0,s(x)s(x)表示,x的各数位之和,
例如:
s(11)=1+1=2s(11)=1+1=2
s(123)=1+2+3=6s(123)=1+2+3=6
s(256)=2+5+6=13s(256)=2+5+6=13
接下来,给你一个正整数n,让你求正整数 x!存在输出x,不存在就输出-1;
真心数学题。
分析:
x2 + s(x)⋅x − n = 0(0)(0)x2 + s(x)·x − n = 0
x2+s(x)⋅x−n+s(x)24=s(x)24(1)(1)x2+s(x)·x−n+s(x)24=s(x)24
(x+s(x)2)2=s(x)24+n(2)(2)(x+s(x)2)2=s(x)24+n
x+s(x)2=s(x)24+n−−−−−−−√(3)(3)x+s(x)2=s(x)24+n
x=s(x)24+n−−−−−−−√−s(x)2(4)(4)x=s(x)24+n−s(x)2
高中学习过的一元二次方程的配方~
接下来,分析数据,1 ≤ n ≤ 10181 ≤ n ≤ 1018
x2 + s(x)⋅x − n = 0(0)(0)x2 + s(x)·x − n = 0
x2 + s(x)⋅x = n (5)(5)x2 + s(x)·x = n
方程0 —>>方程5 不解释;
由方程5得:x2x2 与 nn 数量级相同
即:
即:1 ≤ x≤ 10181/2=1091 ≤ x≤ 10181/2=109
即:x的最大值为999999999(9个9)
那么:s(x)s(x)的最大值为9∗9=819∗9=81,最小值为11
综上:结合方程4 ,n为给定值,遍历区间[1,81][1,81],只有x为未知量,分别求解出81个x,带入原方程0中,判断下就ok了!
重新上了一遍高中的数学课。。。。
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#define maxn 100005
#define ll long long
#define debug cout<<"***********"<<endl;
using namespace std;
int sumDigit(ll n){
int sum = 0;
while(n){
sum += n%10;
n /= 10;
}
return sum;
}
int main(){
ll n;cin>>n;
bool flag = false;
ll sumX = 0;
for(int i = 1; i < 82; i++){
sumX = sqrt( (double)i*i/4 + n) - i/2;
if(sumX * sumX + sumDigit(sumX)*sumX - n == 0){
flag = true;
break;
}
}
if(flag){
cout<<sumX<<endl;
}
else{
cout<<"-1"<<endl;
}
return 0;
}
加油少年!