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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Small Multiple(AtCoder-3621)

發(fā)布時(shí)間:2025/3/17 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Small Multiple(AtCoder-3621) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Problem Description

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

  • 2≤K≤105
  • K?is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Example

Sample Input 1

6

Sample Output 1

3
12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5
11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36

題意:給出一個(gè)整數(shù) k,求這個(gè)數(shù)的倍數(shù),使得其各位數(shù)的和最小,并輸出這個(gè)和

思路:bfs

設(shè)答案為 x,由于要求 x 的各位數(shù)之和最小,那么可以從 1 開始,當(dāng)搜索到的第一個(gè)值 %k 為 0 即為答案

那么有 2 種入隊(duì)方式:

  • x+1:此時(shí)各位數(shù)字和比之前的多了一個(gè)?1,那么代價(jià)是 1
  • x*10:此時(shí)各位數(shù)字和比之前沒有改變,那么代價(jià)就是 0

因此問題轉(zhuǎn)換為用 bfs 找到滿足條件的 x,由于要找各位和最小的,也就是說代價(jià)為 0 的首先出隊(duì),因此可以使用雙端隊(duì)列,將代價(jià)為 0 的放在隊(duì)首,代價(jià)為 1 的放在隊(duì)尾?

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> #include<bitset> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 100000+5; const int dx[] = {-1,1,0,0,-1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std; int vis[N]; int main() {int n;scanf("%d",&n);deque<pair<int,int> > Q;Q.push_front(make_pair(1,1));while(!Q.empty()){pair<int,int> temp=Q.front();Q.pop_front();if(!vis[temp.first]){vis[temp.first]=true;if(temp.first==0){printf("%d\n",temp.second);break;}Q.push_front(make_pair((temp.first*10)%n,temp.second));Q.push_back(make_pair((temp.first+1)%n,temp.second+1));}}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的Small Multiple(AtCoder-3621)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。