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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

桁和 / Digit Sum(AtCoder-2038)

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

Problem Description

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?bdenotes 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≤10^11
  • 1≤s≤10^11
  • 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.

Example

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1

題意:給出一個數字 n,其在進制 b 下的各位數字的和為 s,現在給出 n 和 s,求 b 是否存在,若存在輸出最小值,若不存在,輸出 -1

思路:

首先考慮 s 與 n 的關系:

  • s=n 時,最小的 b 是 s+1
  • s>n 時,無解,輸出 -1
  • s<n 時,需要討論 b 與 n 的關系

設數字 n 的位數為 m,其各位的值為:

那么,根據題意,有:

由于數字位數的特性,那么可以討論 b 與 n 的關系,假設 n 在 b 進制下最高次冪為 2,那么就有 ,即討論如下兩種情況:

  • ?時:b 進制的最高次冪為二次冪及以上,暴力枚舉
  • ?時:b 進制的最高次冪為二次冪以下,有:,化簡得:,于是枚舉 x1 判斷 b 是否合法即可

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 50+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std;int main() {LL n,s;scanf("%lld%lld",&n,&s);if(n==s)printf("%lld\n",s+1);else if(n<s)printf("-1\n");else {LL mid=sqrt(n)+1;bool flag=false;//b>sqrt(n)LL res;for(LL i=2; i<=mid; i++) { //枚舉bres=0;LL temp=n;while(temp) {res+=temp%i;temp/=i;}if(res==s) {printf("%lld\n",i);flag=true;break;}}//b<=sqrt(n)if(!flag) {LL factor=n-s;LL res;mid=sqrt(factor)+1;for(LL i=mid; i>=1; i--) {//枚舉x1if(factor%i==0) {res=factor/i+1;LL j=s-i;//x2if(i<res&&res>=2&&j<res&&j>=0) { //判斷b的合法性printf("%lld\n",res);flag=true;break;}}}}//其他情況if(!flag)printf("-1\n");}return 0; }

?

總結

以上是生活随笔為你收集整理的桁和 / Digit Sum(AtCoder-2038)的全部內容,希望文章能夠幫你解決所遇到的問題。

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