Problem 2125 简单的等式
2013-11-28 23:18
330 查看
Problem 2125 简单的等式
Accept: 96 Submit: 375
现在有一个等式如下:x^2+s(x,m)x-n=0。其中s(x,m)表示把x写成m进制时,每个位数相加的和。现在,在给定n,m的情况下,求出满足等式的最小的正整数x。如果不存在,请输出-1。
有T组测试数据。以下有T(T<=100)行,每行代表一组测试数据。每个测试数据有n(1<=n<=10^18),m(2<=m<=16)。
输出T行,有1个数字,满足等式的最小的正整数x。如果不存在,请输出-1。
4 4 10 110 10 15 2 432 13
-1 10 3 18
福州大学第十届程序设计竞赛
Accept: 96 Submit: 375
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
现在有一个等式如下:x^2+s(x,m)x-n=0。其中s(x,m)表示把x写成m进制时,每个位数相加的和。现在,在给定n,m的情况下,求出满足等式的最小的正整数x。如果不存在,请输出-1。
Input
有T组测试数据。以下有T(T<=100)行,每行代表一组测试数据。每个测试数据有n(1<=n<=10^18),m(2<=m<=16)。
Output
输出T行,有1个数字,满足等式的最小的正整数x。如果不存在,请输出-1。
Sample Input
4 4 10 110 10 15 2 432 13
Sample Output
-1 10 3 18
Source
福州大学第十届程序设计竞赛// 因为 s(x,m) 不是很大,所以我们可以枚举 s(x,m) , // 然后二次方程求根,最后判断是不是对的 #include<iostream> #include<string> #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<algorithm> #define maxn 50002 #define LL long long using namespace std ; int get( LL a , LL m ) { int ans = 0 ; while(a) { ans += a%m ; a /= m ; } return ans ; } int main() { int i ,k ,j ; LL n , m , ans ; // freopen("in.txt","r",stdin) ; cin >> k ; while( k-- ) { cin >> n >> m ; for( i = 0 ; i < 900 ;i++ ) { ans = (LL)((sqrt(i*i+4*n)-i)/2) ; if(ans*ans+get(ans,m)*ans-n == 0 ) break ; } if( i == 900 ) puts("-1") ; else cout << ans << endl; } return 0 ; }
相关文章推荐
- fzu——Problem 2125 简单的等式
- FZU 2125 Problem 2125 简单的等式
- fzu Problem 2125 简单的等式
- Problem 2125 简单的等式(FZU),,数学题。。。
- FZU - 2125 - 简单等式
- FZU 2125 简单的等式 枚举解方程式
- 福州大学第十届校赛 & fzu 2125简单等式
- FZU 2125 简单的等式
- FZU 2125 简单的等式
- FZU 2125 简单的等式
- FZU 2125 简单的等式 (数学+枚举)
- fzu 2125 简单的等式
- FZU 2125 简单的等式【枚举】
- 福州大学第十届程序设计竞赛+Problem E 简单的等式
- FZU-2125- 简单的等式
- FZU2125 简单的等式(枚举)
- FZU 2125 简单的等式
- HDU 1022 Train Problem I(栈的简单应用)
- HDU 4627 The Unsolvable Problem(简单题)
- uva 11991 Easy Problem from Rujia Liu?(数据结构:map的简单用法)