arc084_b (思维+最短路)(好题)
2017-11-07 20:07
302 查看
题目
传送门D - Small Multiple
Time limit : 2sec / Memory limit : 256MB
Problem Statement
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.
Samples
Input | Output | Statement |
---|---|---|
6 | 3 | 12=6×2 yields the smallest sum |
41 | 5 | 11111=41×271 yields the smallest sum |
79992 | 36 |
分析
题意就是给一个数 k ,问他的正整数倍数中,(十进制下)每一位的和最小是多少。这题也是一道通过建图求最短路的思维好题。
新建一个图,共 k 个节点,代表 模k 得到的值。
可以先建好一个图,建边的依据是已知一个数
x%k=p,那么在 x 后面再加一位数得到 y ,可以明确地知道
y%k的值,于是就得到了一个图,每条边代表往后加一位,边权就是这一位的数字。
跑一边最短路,其中 dis[i] 意义就是模 k 余 i 的数中每一位的和最少是多少,开始
1~9都加入队列(它们为第一位都可以),跑一次 SPFA ,最后输出 dis[0] 即可。
程序
#include <cstdio> #include <cstring> #define For(x) for(int h=head[x],o=V[h],w=W[h]; h; o=V[h=to[h]],w=W[h]) using namespace std; int head[100005],to[1000005],V[1000005],W[1000005],num; int q[10000005],l,r,f[100005],dis[100005]; int k; void Add(int x,int y,int z){ to[++num]=head[x],head[x]=num,V[num]=y,W[num]=z; } void SPFA(){ memset(dis,0x7f,sizeof(dis)); for (int i=1; i<10; i++) q[++r]=dis[i]=f[i]=i; //起始位(及最终数的最高位)是 1~9 都可以,都要加入队列中 for (l=1; l<=r; l++){ For(q[l]) if (dis[o]>dis[q[l]]+w){ dis[o]=dis[q[l]]+w; a740 if (!f[o]){ f[o]=1; q[++r]=o; } } f[q[l]]=0; } } void debug(){ for (int i=0; i<k; i++){ printf("%d\t",i); For(i) printf("(%d %d) ",o,w); puts(""); } puts(""); } int main(){ scanf("%d",&k); for (int i=1; i<k; i++) for (int j=0; j<10; j++) Add(i,(i*10+j)%k,j); //每个模数往后加一个位(0~9都可),可以形成一个到另一个模数的边 SPFA(); printf("%d",dis[0]); //通过重重操作,最后得到模数为 0 (k的正整数倍)的路径长就是每一位的和 }
相关文章推荐
- Codeforces 667D World Tour【最短路+思维】好题!
- 51nod 1444 破坏道路【思维+最短路+枚举】这种题好套路啊
- FZU 2169 shadow【最短路+思维】
- Codeforces 544D Destroying Roads【思维枚举+最短路】好题~好题!
- 2017 Multi-University Training Contest 9 && HDU 6166 Senior Pan 【最短路+思维】
- Codeforces 716D Complete The Graph【思维+最短路】
- HDU 6026 Deleting Edges【最短路】【思维题】
- SDUT 3562 Proxy【思维+最短路SPFA】
- 河南多校暑期训练-恢复赛 <字典树,最短路,queue,map,模拟,思维>
- zzuli 2179 紧急营救【最短路+思维】
- HDU 6071 最短路思维题
- HDU 3832 Earth Hour 【最短路好题 + 思维】
- UVALive-7220 Dungeon Trap【最短路Bfs+思维枚举】好题~
- Hdu 6166 Senior Pan【思维+随机化+最短路】好题~
- Codeforces 602C The Two Routes【最短路+思维】
- CodeForces - 786B Legacy(线段树 +最短路+思维好题)
- Heavy Transportation POJ - 1797 --- 最短路思维的最大承载量(最大生成树)
- [ARC084]D - Small Multiple 图论建模+最短路
- 51nod1649 齐头并进 (思维,最短路)
- HDU 6166 Senior Pan(思维 最短路)