zoukankan      html  css  js  c++  java
  • Digit Sum II( ABC044&ARC060)

    问题 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+1);
    	else
    	{
    		for(ll i=2;i<=sqrt(n)+1;i++)
    		{
    			ll b=i;
    			if(sum(n,b)==s)
    			{
    				return 0*printf("%lld
    ",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
    ",b);
    					return 0;
    				}
    			}
    		}
    		puts("-1");
    	}
    	return 0;
    }
    

      




  • 相关阅读:
    HashMap的负载因子为什么默认是0.75?这篇文章告诉你答案
    如何设计短网址系统?
    前端HTML史上最全总结,入门级边学边实战!
    阿里2021最全新的java面试题总结
    三万字,100题!Linux知识汇总!
    程序员路线图
    基于nodejs的selenium的自动化测试之网络元素
    Electron桌面程序入门研究
    Git常用命令
    二次探测法
  • 原文地址:https://www.cnblogs.com/lemon-jade/p/9108142.html
Copyright © 2011-2022 走看看