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 ...
随机推荐
- 【做题】CF285E. Positions in Permutations——dp+容斥
题意:求所有长度为\(n\)的排列\(p\)中,有多少个满足:对于所有\(i \,(1 \leq i \leq n)\),其中恰好有\(k\)个满足\(|p_i - i| = 1\).答案对\(10^ ...
- CF983B XOR-pyramid
设\(xorx[l][r]\)表示题目中\(f(l,r)\)的值,则可以得出 \[ xorx[i][j]=xorx[i][j-1] \oplus xorx[i+1][j] \] 设\(maxx[l][ ...
- C++变量的默认初始化规则
定义没有初始化式的变量时,系统有时候会帮我们初始化变量.系统如何初始化取决于变量的类型以及变量定义的位置. 内置类型变量是否自动初始化取决于变量定义的位置.函数体外定义的变量初始成0:函数体内定义的变 ...
- JVM 及 垃圾回收机制原理
JVM Java 虚拟机 Java 虚拟机(Java virtual machine,JVM)是运行 Java 程序必不可少的机制.JVM实现了Java语言最重要的特征:即平台无关性.原理:编译后的 ...
- Quartus工程中各文件类型的含义
https://blog.csdn.net/jingliangliu/article/details/52245497 .jic JTAG Indirect Configurati ...
- OpenLayers中的球面墨卡托投影
最近看OpenLayers,研究到地图投影时找到官方的文档,就翻译了一下,由于英文能力差,翻译不好的地方,请看原文 原文地址:http://docs.openlayers.org/library/sp ...
- uiautomatorviewer错误 unable toconnect to adb
报错信息: 如图为adb所在路径: 编辑uiautomatorviewer.bat文件,修改最后一行, 修改前: call "%java_exe%" "-Djava.ex ...
- Windows下pipenv将虚环境文件的位置设置在项目根目录下
在windows下使用pipenv shell时,虚拟环境文件夹会在C:\Users\Administrator\.virtualenvs\目录下默认创建,为了方便管理,将这个虚环境的文件的位置更改一 ...
- 滑动验证 和滑动图片验证JS
滑动验证 先放效果图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- less 前端css利器 工程化、逻辑化 css 文件
less LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. 1. 浏览器方式 1.1 html <!DOCTYPE html> <html lang=&q ...