HDU 5710 Digit-Sum

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情


5*2=10,S(5*2)=1,2*S(5)=10;
6*2=12,S(6*2)=3,2*S(6)=12;
7*2=14,S(7*2)=5,2*S(7)=14;
8*2=16,S(8*2)=7,2*S(8)=16;
9*2=18,S(9*2)=9,2*S(9)=18;

规律显然,其实就是满十进1,每位数字之和便小了9。


假设n里有L位数为5-9,那么显然满足:S(2n)=S(n)*2-L*9


那么a*S(n) == b*(2S(n)-L*9)
化简:(2b-a)*S(n) == 9*L*b

关键:9*b:2b-a = S(n):L


到此可以推出三个结论

if (L==0) {printf ("1\n"); continue;}   //L=0时,S(2)=2*S(1)
if (L<0) {printf ("0\n"); continue;}   //(2b-a)*S(n) == 9*L*b,L不可能小于0
if (5*L>S) {printf ("0\n"); continue;}   //假设就是5-9部分有L位,如果最小的5乘以L都会爆,那么不符合假设

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<sstream>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
int gcd (const LL &a, const LL &b) {return b==0?a:gcd (b,a%b);}
using namespace std;
int ans[5000];
int main() {
    int T,a,b; scanf ("%d",&T);
    while (T--) {
        scanf ("%d%d",&a,&b);
        int L=2*b-a;
        int S=9*b;
        //(2b-a)*S(n) == 9*L*b
        if (L<0) {printf ("0\n"); continue;}
        if (5*L>S) {printf ("0\n"); continue;}
        if (L==0) {printf ("1\n"); continue;}
        int g=gcd (L,S);
        L/=g; S/=g;
        //因为化简到最后是比值形式,所以同除最大公约数,到达最小
        
        int bitcnt=0; S-=5*L;
        //总和减掉555...5 之后,剩的那部分数字之和
        
        for (int i=0; i<L; ++i) {
            int mod=min (4,S);//从个位开始,使5变成9,这样能使9尽可能减小
            S-=mod;
            ans[bitcnt++]=5+mod;
        }
        
        //如果L位都是9,结果还有剩余,那么高位从4开始往前填写
        while (S) {
            int oth=min (4,S);//5-9 is used,就假设L位数为5-9,剩的肯定是1-4啊
            ans[bitcnt++]=oth;
            S-=oth;
        }
        for (int i=bitcnt-1; i>=0; --i) {
            printf ("%d",ans[i]);//逆序输出即可
        }
        puts ("");
    }
    return 0;
}


Digit sum Digit sum 描述 原题链接Digit sum 意思就是给出一个数n,把1~n的每个数的b进制求出,再把每个b进制数的每一位相加求和,最后把每一个和相加求和就是所求的结果。 这个题非常简单,但我提交三次都是运行超时,下面是超时的代码: //运行超时 #include <iostream> #include <cstdio> using namesp... 阅读详情

相关推荐

HDU - 5710—— Digit-Sum

Sample Input 3 2 1 4 1 3 4 Sample Output   1 0 55899   需要注意,当 a&gt;2×b以及 5×a&lt;b时,不可构造,输出  #include &lt;bits/stdc++.h&gt; typedef long long LL; using namespace std; int GC...

小宫主的博客 347

Digit sum【暴力+打表】

Digit sum 33.57% 2000ms 131072K A digit sum S_b(n)S b ​ (n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 + 3 = 8S 10(233)=2+3+3=8, S_{2}(8)=1 + 0 + 0 = 1S 2 ​ (8)=1+0+0=1,...

C++的星空 626

【白书之路】1583 - Digit Generator

1583 - Digit Generator For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the

colorfulshark 608

紫书第三章训练 UVA 1583 Digit Generator by 16 BobHuang

For a positive integerN, the digit-sum ofNis defined as the sum ofNitself and its digits. WhenMis the digitsum ofN, we callNagenerator ofM. For example, the digit-sum of245is256 (= 2...

weixin_30848775的博客 190

hdu 5710 Digit-Sum (构造题)

题目链接:hdu 5710 Digit-Sum 题意: 定义S(n)=n的数位和,给你一个a,b让你找一个n使得a*S(n)=b*S(2n)。 题解: 传送门 构造题很少做啊!! 太弱了 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 #define ___ ...

weixin_30670151的博客 168

HDU 5710 Digit-Sum 数学杂题

原题见HDU 5710题意:定义S(N)S(N)是NN的数位之和,给出一对a,b(0<a,b<101)a,b(0<a,b<101),求是否存在nn满足a×S(n)=b×S(2n)a\times S(n)=b\times S(2n)分析:S(n)与S(2n)S(n)与S(2n)的关系如何?对于n中的任何一个数位x,若x为0-4,则因为没有进位,所以在S(2n)中贡献为2x;若x为5-9,则由于其超过1

Ugly Gardon 1987

HDU-5710 Digit-Sum

HDU-5710 Digit-Sum AC代码及题解

月凉酱的博客 342

hdu 5710 digitsum数论

题目链接 hdu 5710 思路题 /*竟然一道题想了两天 对于n中的任何一个数位x,若x为0-4,则因为没有进位, 所以在S(2n)中贡献为2x;若x为5-9,则由于其超过10,在S(2n)中贡献为2x-10+1. a*S=b*(2S*9L)(2b-a)S=9*b*L s=9bl/(2b-a); s就是s(n),a,b就是题目中给的ab l是某一位上为5-9的 位数 1. a=2b,则...

candiceyj的博客 226

[HDU 5710]Digit-Sum:其他

点击这里查看原题可以从*2后各位数和的变化来考虑,如果某一位≥5,那么 *2后必然会进位,于是本位-10,前一位+1,总共减少9,因此我们只需要考虑两个问题: 数字总和应该是多少? 应该有多少位>4? 其实这两个问题可以同时解决,设S(n)=x,则S(2n)=2x-9k,k为正整数,那么,(2b-a)/9b*x一定为正整数,因此,可以设c=2b-a,d=9b,求gcd(c,d),则S(n)=d/gc

SmallSXJ的博客 691

HDU 5710 digit sum

题意: 定义S(N) 为数字N每个位上数字的和。 在给两个数a,b,求最小的正整数n,使得 a×S(n)=b×S(2n)。 分析: 为我们要找到满足 a * S(n) = bS(2n) 的最小的n的值,首先第一个想法肯定暴力啊..但是暴力肯定不对啊... 比如--->2 3 输出5589 也就是说从整体(一个数字)到部分(各个位数)的思路是行不通的,因为是digit 所以可以尝试由...

aocai9291的博客 121

HDU5710-Digit-Sum

题目传送门 题目大意: 定义S(N) 为数字N每个位上数字的和。在给两个数a,b,求最小的正整数n,使得 a×S(n)=b×S(2n) 思路在代码中会详细解说,请看代码#include<cstdio> #include<algorithm> #include<cstring> using namespace std; int ans[100]; int tot; int gcd(int n,i

kaven 4632

hdu 5710 Digit-Sum (推公式)

题目链接 题意: S(n)为n的各个位的数字之和,给出a,b,求最小的n值(n为正整数),使得a*S(n)=b*S(2*n),没有的话输出0 思路: 首先,1-9的S(n)值和S(2*n)值如下: n S(n) S(2*n) S(2*n)-S(n) 1 1 ...

暖心的博客 496

Hdu 5710 Digit-Sum【思维】

Digit-Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 486    Accepted Submission(s): 148 Problem Description Let S(N) be digi

mengxiang000000 602

HDU 5710 Digit Sum

LetS(N)S(N)be digit-sum ofNN, i.eS(109)=10,S(6)=6S(109)=10,S(6)=6.If two positive integersa,ba,bare given, find the least positive integernnsatisfying the conditiona×S(n)=b×S(2n)a×S(n)=b×S(2...

weixin_30657999的博客 110

hdu5710 Digit-Sum(构造+贪心)

题目 S(n)定义为一个数的各数位之和 给定a,b(0&lt;a,b&lt;101), 求满足a*S(n)==b*S(2n)的最小的n 如果无解输出0 思路来源 https://www.cnblogs.com/wenruo/p/5562100.html https://blog.csdn.net/DorMOUSENone/article/details/71223368 题解 如...

小衣同学的博客 452

Digit-Sum HDU - 5710

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,b are given, find the least positive integer nnsatisfying the condition a×S(n)=b×S(2n)a

dsaghjkye的博客 407
上一篇: HDU 5706 GirlCat
下一篇: hdu 5443 The Water Problem
Kim0403
博客等级 码龄12年 25粉丝 132原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值