Problem Description

For integers b(b≥2) and n(n≥1), let the function f(b,n) be defined as follows:

  • f(b,n)=n, when n<b
  • f(b,n)=f(b, floor(nb))+(n mod b), when nb

Here, floor(nb) denotes the largest integer not exceeding nb, and n mod bdenotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b. For example, the following hold:

  • f(10, 87654)=8+7+6+5+4=30
  • f(100, 87654)=8+76+54=138

You are given integers n and s. Determine if there exists an integer b(b≥2) such that f(b,n)=s. If the answer is positive, also find the smallest such b.

Constraints

  • 1≤n≤10^11
  • 1≤s≤10^11
  • n, s are integers.

Input

The input is given from Standard Input in the following format:

n
s

Output

If there exists an integer b(b≥2) such that f(b,n)=s, print the smallest such b. If such b does not exist, print -1 instead.

Example

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1

题意:给出一个数字 n,其在进制 b 下的各位数字的和为 s,现在给出 n 和 s,求 b 是否存在,若存在输出最小值,若不存在,输出 -1

思路:

首先考虑 s 与 n 的关系:

  • s=n 时,最小的 b 是 s+1
  • s>n 时,无解,输出 -1
  • s<n 时,需要讨论 b 与 n 的关系

设数字 n 的位数为 m,其各位的值为:

那么,根据题意,有:

由于数字位数的特性,那么可以讨论 b 与 n 的关系,假设 n 在 b 进制下最高次幂为 2,那么就有 ,即讨论如下两种情况:

  •  时:b 进制的最高次幂为二次幂及以上,暴力枚举
  •  时:b 进制的最高次幂为二次幂以下,有:,化简得:,于是枚举 x1 判断 b 是否合法即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 50+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;int main() {LL n,s;scanf("%lld%lld",&n,&s);if(n==s)printf("%lld\n",s+1);else if(n<s)printf("-1\n");else {LL mid=sqrt(n)+1;bool flag=false;//b>sqrt(n)LL res;for(LL i=2; i<=mid; i++) { //枚举bres=0;LL temp=n;while(temp) {res+=temp%i;temp/=i;}if(res==s) {printf("%lld\n",i);flag=true;break;}}//b<=sqrt(n)if(!flag) {LL factor=n-s;LL res;mid=sqrt(factor)+1;for(LL i=mid; i>=1; i--) {//枚举x1if(factor%i==0) {res=factor/i+1;LL j=s-i;//x2if(i<res&&res>=2&&j<res&&j>=0) { //判断b的合法性printf("%lld\n",res);flag=true;break;}}}}//其他情况if(!flag)printf("-1\n");}return 0;
}

桁和 / Digit Sum(AtCoder-2038)相关推荐

  1. Digit Sum II( ABC044ARC060)

    问题 G: Digit Sum II 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 11 [提交][状态][讨论版][命题人:admin] 题目描述 For intege ...

  2. Digit sum【暴力+打表】

    Digit sum 33.57% 2000ms 131072K A digit sum S_b(n)S b ​ (n) is a sum of the base-bb digits of nn. Su ...

  3. 2018.09.17 atcoder Digit Sum(数论)

    传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...

  4. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...

  5. AtCoder 杂题训练

    前言: 因为要普及了,今年没一等就可以退役去学文化课了,所以暑假把历年noip普及组都刷了一遍,离noip还有50+天,想弄点强化训练什么的. 想了想,就这些天学文化课之余有空就把AtCoder之前那 ...

  6. AtCoder Grand Contest 021完整题解

    提示:如果公式挂了请多刷新几次,MathJex的公式渲染速度并不是那么理想. 总的来说,还是自己太弱了啊.只做了T1,还WA了两发.今天还有一场CodeForces,晚上0点qwq... 题解还是要好 ...

  7. AtCoder Beginner Contest 208

    文章目录 AtCoder Beginner Contest 208 A - Rolling Dice B - Factorial Yen Coin C - Fair Candy Distributio ...

  8. 狂补dp Atcoder Educational DP Contest(全)

    狂补dp Atcoder Educational DP Contest(全) 注意 为了减少篇幅,本博客代码部分只放主代码部分,其余省略快读.快输.头文件等代码,即把代码看做伪代码更佳 A - Fro ...

  9. atcoder题目合集(持续更新中)

    Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...

最新文章

  1. 速率单位和信息量单位区分
  2. 书评 | 9 年码龄工程师读 Android 经典
  3. Unity镜子效果的实现(无需镜子Shader)
  4. 模组使用之dB、dBm、dBw、dBu、dBc的含义和之间的关系
  5. C语言实验——拍皮球_JAVA
  6. include require区别
  7. leetcode 792. Number of Matching Subsequences | 792. 匹配子序列的单词数(Java)
  8. 苹果第二代自研M系列芯片MacBook Pro有望在未来几周上市
  9. app商城源码_海量的SpringBoot和SSM项目【附带源码+视频教程】快速成为全栈
  10. AS3 使用Loader对象加载外部图片
  11. ubuntu创建切换超级用户
  12. 大学计算机实验报告2,大学计算机基础实验报告2.doc
  13. 项目如何开始:怎样和客户谈需求(转)
  14. python找到二维数据矩阵中的最大最小值直接使用min、max函数
  15. 公开课丨苏杰新著来袭,十年之后再谈人人都是产品经理!
  16. 32 位和 64 位版本的 Office 2010 之间的兼容性
  17. adb shell ping
  18. 【高等数学】曲线的切线与法平面和曲面的切平面与法线
  19. 手游用户数据分析平台
  20. java clh_Java多线程编程CLH锁详解

热门文章

  1. 果断收藏!156页PPT全景展现全球区块链发展(附完整版下载手册)
  2. Simulink之门极关断晶闸管(GTO)
  3. rust如何进枪战服_rust手机版
  4. 如何解决 IDEA 占用大量 CPU 导致的卡顿问题?这顿操作猛如虎
  5. 线上服务被干爆了,竟然是日志的锅!!
  6. 为什么阿里巴巴Java开发手册中强制要求接口返回值不允许使用枚举?
  7. 基于微信我们可以做什么样的应用?
  8. 【实战项目】采用jeecg3.2框架,一个人写的购物系统
  9. LOG4J原理以及配置的研究
  10. linux下源码安装官方最新版Python