问题 G: Digit Sum II

时间限制: 1 Sec  内存限制: 128 MB
提交: 36  解决: 11
[提交][状态][讨论版][命题人:admin]

题目描述

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(n⁄b))+(n mod b), when n≥b
Here, floor(n⁄b) denotes the largest integer not exceeding n⁄b, and n mod b denotes 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≤1011
1≤s≤1011
n,s are integers.

输入

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

输出

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.

样例输入

87654
30

样例输出

10

题意:已知 n,s ,n 转化成b 进制数,且各位数之和为s, 求这个最小的b ,若不存在,输出 -1 

c++ code:

#include <bits/stdc++.h>using namespace std;
typedef long long ll;
ll sum(ll n,ll b)
{ll ans=0;while(n){ans+=n%b;n/=b;}return ans;
}
int main()
{ll n,s;scanf("%lld%lld",&n,&s);if(n==s)printf("%lld\n",n+1);else{for(ll i=2;i<=sqrt(n)+1;i++){ll b=i;if(sum(n,b)==s){return 0*printf("%lld\n",b);return 0;}}if(n-s>1){for(ll i=sqrt(n);i;i--){ll b=(n-s)/i+1;if(sum(n,b)==s){printf("%lld\n",b);return 0;}}}puts("-1");}return 0;
}

  


转载于:https://www.cnblogs.com/lemon-jade/p/9108142.html

Digit Sum II( ABC044ARC060)相关推荐

  1. PAT (Advanced Level) Practice 题解代码 - II (1051-1100)

    PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...

  2. WM有约II(二):持续改进

    WM有约II(二):持续改进 Written by Allen Lee 自定义"你在干嘛?" 在上一集结束的时候,我们发现一个极其严重的问题--这个应用程序会忽略我们最初希望截获的 ...

  3. WM有约II(一):你在干嘛?

    WM有约II(一):你在干嘛? Written by Allen Lee "你在干嘛?" 我想大多数读者都收到过这样的短信息吧?有时候我在想,能否为手机开发一个应用程序,自动回复当 ...

  4. 数论 II(组合数学)

    数论 II(组合数学) >> 排列数 定义 排列(ArrangmentArrangmentArrangment)数指的是从 nnn 个不同元素中任取 m(m⩽n)m\ (m\leqslan ...

  5. LeetCode 07: T48. 旋转图像 (中等); T54. 螺旋矩阵(中等); T240. 搜索二维矩阵 II (中等)

    文章目录 T19: 48. 旋转图像 (中等) 思路 解法 1: 新增数组 解法 2: 原地旋转 T20: 54. 螺旋矩阵(中等) 思路 解法: 到边界换方向 T21: 240. 搜索二维矩阵 II ...

  6. 探索 Word 2007 开发 II(二):引用 Amazon 图书信息

    探索 Word 2007 开发 II(二):引用 Amazon 图书信息 Written by Allen Lee 引经据典 写文章的时候,我喜欢引经据典,只要有需要,我就会引用曾经看过的图书.然而, ...

  7. Scrapy研究探索(六)——自动爬取网页之II(CrawlSpider)

    原创,转载注明: http://blog.csdn.net/u012150179/article/details/34913315 基于上面的博客修改而得 一 目的 在教程(二)中使用基于Spider ...

  8. LeetCode—52. N皇后 II(困难)

    52. N皇后 II(困难) 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n × n 的棋盘上,并且使皇后彼此之间不能相互攻击.即令其中任意两个皇后都不同列.同行和在一条斜线上. 给你一 ...

  9. 剑指Offer 14- I. 剪绳子(Medium)/ 剪绳子 II(Medium)/ 343. 整数拆分(Medium)

    剑指Offer 14- II. 剪绳子 II(Medium) 343. 整数拆分(Medium) [题目连接] 题解 剪绳子(数学推导 / 贪心思想,清晰图解) 图解[暴力递归][记忆化技术][动态规 ...

最新文章

  1. 机器翻译pascal程序
  2. hadoop yarn的三种资源调度器详解
  3. Drainage Ditches - poj 1273(网络流模板)
  4. .NET 指南:安全编码概览
  5. 广发银行创新“智慧金融”打造“智慧城市”
  6. Web系统测试Web安全性测试
  7. dnf助手服务器内部出错,地下城与勇士TGP的DNF助手异常解决办法 TGP-DNF助手补丁不适配怎么办...
  8. 微带滤波器摘要_微带线带通滤波器设计
  9. html5 调速 视频播放器,7款很棒的 HTML5 视频播放器
  10. Java线程状态以及 sheep()、wait()、yield() 的区别
  11. 教你几个白嫖百度文库的方法,亲测可用!建议收藏!
  12. MongoDB studio3T 破解
  13. 安卓镜像刻录软件_手机iso刻录工具去广告版下载-安卓手机版iso刻录工具无广告版(iso写盘工具)v3.4 2020最新版_新绿资源网...
  14. 如何增加(软件测试)自动化测试面试成功率?只需学会主流设计模式PO+关键字驱动!
  15. webrtc音频QOS方法三(回声的产生及抑制)
  16. 超级表格pk王者农药,今天你五杀了么?
  17. C#自定义控件添加至工具箱的方法
  18. Composer -- PHP依赖管理的新时代
  19. 网页占满整个屏幕_html如何让body占满整个屏幕
  20. JMeter递增加压

热门文章

  1. HTML锚点控制,跳转页面后定位到相应位置
  2. KD-Tree复习笔记(BZOJ1941 BZOJ2648 BZOJ4066)
  3. 经常使用排序算法实现[交换排序之冒泡排序、高速排序]
  4. ORACLE 添加删除列脚本
  5. Android之独孤口诀:之强大的“测试单元”
  6. djano-cms学习笔计(一)
  7. 为你详细解读HTTP请求头(header)的具体含意
  8. Linux ibm mq 教程,IBM MQ 使用指南
  9. php 的包管理,php composer包管理器
  10. 怎么去除标题_未来健康家:怎么快速祛除甲醛