HDU 5710 Digit-Sum (构造)
题意:
定义S(N) 为数字N每个位上数字的和。
在给两个数a,b,求最小的正整数n,使得 a×S(n)=b×S(2n)。
官方题解:
这道题目的结果可能非常大,所以我们直接枚举n是要GG的。
首先可以有这样的基础性结论:
设gcd(a,b)=g, 我们可以先使得b=b/g, a=a/g
S(n):S(2n)==b:a,那么我们有S(n):S(2n)=b:a。
然后,一个好的做法是,我们研究本质问题。
我们发现,如果一个digit是0~4,那么*2的效益是完全获得的。
如果一个digit的是5~9,那么*2后会损失9的收益。
a*S(n) == b*S(2n),
我们假设有l的长度是[0,4]范围,有L的长度是[5,9]范围
那么显然满足:
S(2n)=S(n)*2-L*9
替换一下——
a*S(n) == b*(2S(n)-L*9)
a*S(n) == 2b*S(n) -L*9*b
(2b-a)*S(n) == L*9*b
即——
9*b:2b-a = S(n):L
也就是说,我们得到了S(n)与L的比例关系。
然后模拟一遍即可。
怎么模拟呢?
我们不妨假设答案n仅有长度为L,且每一位都是5
然后得到了把数位和sum分撒出去。
对于sum余下的数值,我们依次加到尾巴上。
如果sum最后把长度为L的字串都填充为'9'之后,还有剩余,那么在前面贪心填充。
构造题一般是找规律。找到了就恍然大悟了,找不到就……我靠这题怎么这么难!
做题要大胆,细心。
代码:
#include <iostream> using namespace std; // a*s(n)=b*s(2n)
// a*s(n)=b*( 2*s(n)-9*l )
// (a-b*2)*s(n)=-b*9*l
// (b*2-a)/b*9=l/s(n) int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int ans[];
int main()
{
int T;
cin >> T;
while (T--) {
int a, b;
cin >> a >> b;
int l = b * - a;
int sn = b * ;
if ( * l > sn || l < ) {
cout << "" << endl;
continue;
}
if (l == ) {
cout << "" << endl;
continue;
}
int gg = gcd(l, sn);
l /= gg; sn /= gg;
int idx = ;
sn -= * l;
for (int i = ; i < l; ++i) {
int add = min(, sn);
sn -= add;
ans[idx++] = + add;
}
while (sn) {
int res = min(, sn);
ans[idx++] = res;
sn -= res;
}
for (int i = idx-; i >= ; --i) cout << ans[i];
cout << endl;
}
return ;
}
HDU 5710 Digit-Sum (构造)的更多相关文章
- HDU 5710 Digit Sum
Let S(N)S(N) be digit-sum of NN, i.e S(109)=10,S(6)=6S(109)=10,S(6)=6. If two positive integers a,ba ...
- hdu 4961 Boring Sum(高效)
pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...
- 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 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
随机推荐
- MVC @Html.DropDownListFor 默认值
今天在做MVC 的 @Html.DropDownListFor 的时候,本来数据库中读取到的值是HK,但是 @Html.DropDownListFor的起始默认值始终是“请选择国家”,搞了一个下午, ...
- MAC OS Nginx php-fpm相关
Nginx 命令 sudo nginx // 启动Nginx #重新加载|重启|停止|退出 sudo nginx -s reload|reopen|stop|quit #上传文件限制更改: 进入ngi ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ2524——Ubiquitous Religions
Ubiquitous Religions Description There are so many different religions in the world today that it is ...
- How to Send an HTTP Header With Every Request With Spring RestTemplate
In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...
- 【转】奇异值分解(We Recommend a Singular Value Decomposition)
文章转自:奇异值分解(We Recommend a Singular Value Decomposition) 文章写的浅显易懂,很有意思.但是没找到转载方式,所以复制了过来.一个是备忘,一个是分享给 ...
- DSP\BIOS调试Heaps are enabled,but not set correctly
转自:http://blog.sina.com.cn/s/blog_735f291001015t9i.html Heaps are enabled, but the segment for DSP/B ...
- android 电容屏(一):电容屏基本原理篇
平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...
- function 的声明
<?php function test() { echo "abc"; } test(); ?> 结论: 一 编译 a.对 函数声明进行词法分析和语法分析:在语法分析中 ...
- SCOI2009游戏
1025: [SCOI2009]游戏 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1065 Solved: 673[Submit][Status] ...