题意:

定义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 (构造)的更多相关文章

  1. 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 ...

  2. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

  3. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  4. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  5. HDU 1244 Max Sum Plus Plus Plus

    虽然这道题看起来和 HDU 1024  Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...

  6. (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 ...

  7. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

  8. 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 ...

  9. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

随机推荐

  1. Hex string convert to integer with stringstream

    #include <sstream>#include <iostream>int main() { unsigned int x; std::stringstream ss; ...

  2. PHP基础语法3

    文件系统 判断文件是否存在 如果只是判断文件存在,使用file_exists就行,file_exists不仅可以判断文件是否存在,同时也可以判断目录是否存在,从函数名可以看出, is_file是确切的 ...

  3. 选择排序的openMP实现

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #include < ...

  4. UIBezierPath画圆弧的记录

    UIBezierPath通过 - (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)s ...

  5. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-007-给flowl加权限控制<secured>

    States, transitions, and entire flows can be secured in Spring Web Flow by using the <secured> ...

  6. Android:监听ListView

    本文目录 监听ListView点击事件 监听ListView滚动事件 监听ListView点击事件 使用监听器OnItemClickListener package com.example.tests ...

  7. MyBatis的动态SQL操作--插入

    需求:向数据库中插入一条数据 //id,name,sal非空,三个字段都插入 insert into student(id,name,sal) values (?,?,?) //id,name非空,只 ...

  8. Armitage初始化

    Kali2.0 Armitage初始化步骤如下 (1)点击页面的Armitage按钮 (2)提示Metasploit RPC server is not running,是否启动该服务,选择是 (3) ...

  9. Octave下载

    发福利啦,今天下了半天Octave都没下载下来,最后让一个香港的同学帮忙下好传过来的....放到网盘里造福大家 GNU_Octave_3.8.0-6.dmg 链接: http://pan.baidu. ...

  10. 各开源协议BSD,GPL,LGPL,Apache 2.0,mit等简介*

    快速阅读 分类 子分类 开源约定 BSD original BSD license.FreeBSD license.Original BSD license 为所欲为 Apache Licence 2 ...