HDU 5710 Digit Sum
If two positive integers a,ba,b are given, find the least positive integer nn satisfying the condition a×S(n)=b×S(2n)a×S(n)=b×S(2n).
If there is no such number then output 0.
InputThe first line contains the number of test caces T(T≤10)T(T≤10).
The next TT lines contain two positive integers a,b(0<a,b<101)a,b(0<a,b<101).
OutputOutput the answer in a new line for each test case.
Sample Input
3
2 1
4 1
3 4
Sample Output
1
0
55899
题意:S(n)表示的是求数字n十进制下每个数位的数字的和。给你两个常数a,b,让你找到最小的值 n,使得a * S(n) == b * S(2 * n)。
考察S(n)与S(2n)的关系,可以得到,对于组成n的每一位数x,其对S(n)的贡献为x,对S(2n)的贡献 为2x(x < 4)或2x - 9(x >= 5)。 因此,假设 n 中含有大于等于5的数字L个。S(2n) = 2 * S(n) - 9 * L成立。结合题目要求的等式 a * S(n)=b * S(2n),则: a * S(n) = b * (2 * S(n) - 9 * L) ⟺a * S(n) = 2b * S(n) - 9 * b * L ⟺(2b - a) * S(n) = 9 * b * L ⟺S(n) / L = 9 * b / 2b - a 由于S(n), L, 9 * b, 2b - a均为整数,故 S(n) = k * (9 * b) ,L = k * (2b - a)。同时,由于要求 n 最小, 可知 k 取 1 时最小。 因此,首先构造长为L,每个字符均为5(由于L个数字必须大于等于5)的字符串,同时S(n) -= 5 * L。对于多余的S(n),为使得n尽可能小(即串长最小,大的数字尽可能向个位数靠拢),故优先将串尾 的字符增长到9。 若长为L的字符串已经全部为9。此时不得不增加串长,则优先在串首增加4(不在L个数字中的均小于 等于4),直到S(n)全部用尽。 需要注意,当a > 2 * b以及5 * a < b时,不可构造,输出0。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
int ans[]; int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
int main(){
int t;
cin>>t;
for(int i=;i<=t;i++){
int a,b;
cin>>a>>b;
int n=*b,m=*b-a;
if(a==*b){
cout<<""<<endl;
continue;
}
if(a>*b||n<*m){
cout<<""<<endl;
continue;
}
int k=gcd(m,n);
m/=k,n/=k;
n-=*m;
for(int i=;i<m;i++,n-=k){
k=min(,n);
ans[i]=+k;
}
m--;
while(n>){
ans[++m]=;
n-=;
}
if(n) ans[++m]=n;
for(int j=m;j>=;j--){
cout<<ans[j];
}
cout<<endl;
}
return ;
}
HDU 5710 Digit Sum的更多相关文章
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 1244 Max Sum Plus Plus Plus
虽然这道题看起来和 HDU 1024 Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...
- (Problem 16)Power digit sum
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of th ...
- hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)
题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- hdu 4961 Boring Sum(高效)
pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
随机推荐
- 【学习】Hall’s Marriage Theorem
其实是在做题时遇到这个定理的. 这个定理的图论意义是: 对于一个二分图\(G=\{X+Y,E\}\),它满足: \(\forall W \subseteq X, \, |W| \leq |N_G(W) ...
- 快速理解Java中的七种单例模式
饿汉式(推荐) package concurencyv2.chapter1; public class SingletonV2 { private static final SingletonV2 i ...
- hihoCoder week14 无间道之并查集
并查集的基本使用 #include <bits/stdc++.h> using namespace std; ; int n, fa[N]; int cnt, tot; map<st ...
- P3301 [SDOI2013]方程
思路 容斥的挺好的练习题 对于第二个条件,可以直接使m减去suma2,使得第二个条件舍去,然后m再减去n,使得问题转化成有n1个变量要满足小于等于某个数的条件,其他的随便取,求整数解的个数 对n1,以 ...
- Unity3D学习笔记(三十四):Shader着色器(1)
一.GPU:图形处理器,Graphics Processing Unit 显卡的处理器就是图形处理器.与CPU类似. GPU和CPU的区别? 1.CPU主要是为了串行指令设计,GPU则是为了大规模 ...
- Bytom矿池接入协议指南
矿机配置 https://gist.github.com/HAOYUatHZ/a47400bde4a138825faef415387b532c 固件升级 https://service.bitmain ...
- ETCD原理
etcd:从应用场景到实现原理的全方位解读 从etcd的架构开始,深入到源码中解析etcd 1 架构 从etcd的架构图中我们可以看到,etcd主要分为四个部分. HTTP Server: 用于处理用 ...
- VirtuablBox 出错: VERR_SUPLIB_OWNER_NOT_ROOT 解决方法
刚刚把 VirtualBox 升级, 从 3.2 到 4.0.4 后,虚拟机上的系统无法运行, 提示: VERR_SUPLIB_OWNER_NOT_ROOT 查了一下,发现是因为 /opt 的 own ...
- 19. --mus-- 音乐,娱乐(词20)
- 真刀实战地搭建React+Webpack+Express搭建一个简易聊天室
一.前面bb两句 因为自惭(自残)webpack配置还不够熟悉,想折腾着做一个小实例熟悉.想着七夕快到了,做一个聊天室自己和自己聊天吧哈哈.好了,可以停止bb了,说一下干货. 二. 这个项目能学到啥? ...