日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Digit Sum II(ABC044ARC060)

發布時間:2025/7/25 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Digit Sum II(ABC044ARC060) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。