HDU 5710 Digit-Sum

9 篇文章 0 订阅
3 篇文章 0 订阅


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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值