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 ...
随机推荐
- 【特性】Redis4.0新特性
模块系统 Redis 4.0 发生的最大变化就是加入了模块系统, 这个系统可以让用户通过自己编写的代码来扩展和实现 Redis 本身并不具备的功能, 具体使用方法可以参考 antirez 的博文< ...
- python 之 模块
在python模块,是一个python文件,以.py结尾,包含了python对象定义 和python语句 通过import语句 ,语法 import module1[,module2,module3, ...
- 51Nod—1174 区间中最大的数 线段树模版
在大佬们题解的帮助下算是看懂了线段树吧...在这mark下防一手转头就忘. #include<iostream> #include<stdio.h> using namespa ...
- node.js 学习笔记一
2017-05-01 安装node 我没安装,下载即使用.要全局使用的话把node加入到环境变量中即可. 以下命令环境均为 cmd . 体验 体验一: 在命令行输入 node ,即进入 node 程序 ...
- HDU 5844 LCM Walk(数学逆推)
http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...
- jenkins 异常
八月 03, 2017 7:23:54 下午 org.eclipse.jetty.util.log.JavaUtilLog warn 警告: FAILED ServerConnector@29e6eb ...
- java复制文件夹中的所有文件和文件夹到另一个文件夹中
1.复制文件夹 public static void copyDir(String oldPath, String newPath) throws IOException { File file = ...
- MyBatis数据库测试代码自动生成
<!-- generatorConfig.xml配置,其中:<plugin type="org.mybatis.generator.plugins.ToStringPlugin& ...
- Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext
由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包. 结果在运行测试用 ...
- Codeforces Round #271 (Div. 2) F. Ant colony 线段树
F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...