Digit-Sum HDU

来源:互联网 发布:安卓版本 知乎 编辑:程序博客网 时间:2024/05/17 15:22

 

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
32 14 13 4
Sample Output
1055899

 题意很好理解   因为  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");        }}



原创粉丝点击