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×S(n)=b×S(2n)

If there is no such number then output 0. 
Input
The first line contains the number of test caces T(T10)T(T≤10)
The next TT lines contain two positive integers a,b(0<a,b<101)a,b(0<a,b<101)
Output
Output 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) 中 各个位数字设  t , t为 1-4 时 放大2倍 为 2t  , t 而 5-9时 放大2倍 则变为 2t -9  

 设 5 -9的个数为L  那么 s(2n)=2s(n)-9L   则 (a-2b)*s(n)=9bL;    s(n) / l =9b / (a-2b)   那么 a-2b 是 5-9的个数   9b是 s(n)    这样我们得保证 a-2b >=0 且 9b - 5*( a-2b)>0     

我们先假设这 a-2b 个数全为 5   设 p=9b - 5*( a-2b)  ;

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<iomanip>
#include<bits/stdc++.h>
#define eps 1e-8
//#define inf 0x7f
//int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
using namespace std;
int aa[300];
int main()
{
    int t,a,b;
    scanf("%d",&t);
    while(t--)
    {
        memset(aa,0,sizeof(aa));
        scanf("%d%d",&a,&b);
        int k2=2*b-a;
        int k1=9*b;
        if(k2<0||b>5*a) 
        {
            printf("0\n");
            continue;
        }
        if(k2==0)
        {
            printf("1\n");
            continue;
        }
        int u=__gcd(k1,k2);
       k1=k1/u;
       k2=k2/u;
        int i;
        int p=k1-(k2*5);
         for(i=0;i<k2;i++) 
         {
              int e=min(4,p);
              aa[i]=5+e;
              p=p-e;

         }
          while(p)
          {
              int e=min(4,p);
              aa[i++]=e;
              p=p-e;
          }
        for(i=i-1;i>=0;i--)
            printf("%d",aa[i]);
            printf("\n");
        }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值