博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Digit Sum II( ABC044&ARC060)
阅读量:4336 次
发布时间:2019-06-07

本文共 1544 字,大约阅读时间需要 5 分钟。

问题 G: Digit Sum II

时间限制: 1 Sec  内存限制: 128 MB
提交: 36  解决: 11
[][][][命题人:]

题目描述

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.

样例输入

8765430

样例输出

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

             

 

c++ code:

#include 
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

你可能感兴趣的文章
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>