• ARC060 Digit Sum II



    Problem Statement

    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.

    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.


    Sample Input 1

    87654
    30

    Sample Output 1

    10

    题意

    给定一个(n)和一个(s),找到一个最小的(b)使得(n)(b)进制下各位之和为(s).

    分析

    假设(n)表示为(b)进制之后各位上的数字从低位到高位分别为(a_0,a_1,a_2,...),显然我们可以得到如下两个等式

    [a_0+a_1b+a_2b^2+a_3b^3+...=n ag{1} ]

    [a_0+a_1+a_2+a_3+... =s ag{2} ]

    并且对于(forall i in [0,+infty])都有(a_i<b igwedge a_i>=0);

    • 当(1)式中的最高次幂至少二次时,(bleq sqrt{n}),所以我们可以枚举(b)的值从(2)(sqrt n),每次求出(b)进制下各位数字之和看是否等于(s)
    • 当(1)式中的最高次幂为1次时,有$$
      left{
      egin{aligned}
      a_0+a_1b=n
      a_0+a_1=s
      end{aligned}
      ight.

    [化简后有$a_1(b-1)=n-s$,所以$a_1$和$b-1$均为$n-s$的因数.所以我们可以枚举所有$n-s$的因数,并取出所有满足条件的$b$的最小值. + 当最高次幂为0次时,必须要满足$n=s$. ### 代码 ```cpp #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <cmath> using namespace std; typedef long long ll; ll n,s; int main(int argc, char const *argv[]) { scanf("%lld%lld", &n,&s); ll b=1; for (b = 2; b*b <= n; ++b) { ll t=n; ll sum=0; while(t){ sum+=t%b; t/=b; } if(sum==s){ printf("%lld ", b); return 0; } } if(n<s){ printf("-1 "); return 0; } else if(n==s){ printf("%lld ", n+1); return 0; } ll i=1; for (i = 1; i*i <= n-s; ++i) { if((n-s)%i==0){ ll b=i+1; ll a1=(n-s)/i,a0=s-a1; if(a1<b&&a0<b&&a0>=0){ printf("%lld ", b); return 0; } } } for ( ; i >= 1; --i) { if((n-s)%i==0){ ll j=(n-s)/i; ll b=j+1; ll a1=(n-s)/j,a0=s-a1; if(a1<b&&a0<b&&a0>=0){ printf("%lld ", b); return 0; } } } printf("-1 "); return 0; } ```]

  • 相关阅读:
    mac-chrome下手动安装vue-devtools
    python生成随机数、随机字符串
    windows下基于sublime text3的nodejs环境搭建
    解决webstorm卡顿问题
    【更改表单元素默认样式】更改文件上传按钮样式
    JavaScript判断用户是通过电脑端还是移动端访问
    【转载自W3CPLUS】如何将页脚固定在页面底部
    【前端插件】图片上传插件收集
    AngularJS 表单提交后显示验证信息与失焦后显示验证信息
    【转】包管理器Bower详细讲解
  • 原文地址:https://www.cnblogs.com/sciorz/p/9062775.html
Copyright © 2020-2023  润新知