Let S(N) be
digit-sum of N,
i.e S(109)=10,S(6)=6.
If two positive integers a,b are given, find the least positive integer nsatisfying the condition a×S(n)=b×S(2n).
If there is no such number then output 0.
If two positive integers a,b are given, find the least positive integer nsatisfying the condition a×S(n)=b×S(2n).
If there is no such number then output 0.
The next T lines contain two positive integers a,b(0<a,b<101).
3 2 1 4 1 3 4
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");
}
}