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 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 n satisfying 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 n satisfying the condition 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(T≤10)
.
The next T lines contain two positive integers a,b(0<a,b<101) .
The next T lines contain two positive integers 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)表示的是求数字n十进制下每个位子的数字的加和。
给你两个常数a,b,让你找到最小的值n,使得a*S(n)==b*S(2*n);
思路(思路参考自:http://blog.csdn.net/kim0403/article/details/52069813):
S(1)= 1,S(2*1)=2;
S(2)=2,S(2*2)=4;
S(3)=3,S(2*3)=6;
S(4)=4,S(2*4)=8;
S(5)=5,S(2*5)=1;
S(6)=6,S(2*6)=3;
S(7)=7,S(2*7)=5;
S(8)=8,S(2*8)=7;
S(9)=9,S(2*9)=9;
显然,S(2n)=S(n)*2-L*9;
这里L表示的是数字n中,包含5~9这几个数的个数。
那么我们根据等式:
a*S(n)=b*S(2*n);
化简有:
Sn/L=9*b/2b-a;
那么我们设定Sn就是9*b.那么L就是2b-a;
因为这里是成倍数增长的,那么我们将S/gcd(Sn,L),将L/gcd(Sn,L);
得到化简后的S和L.
那么此时的S就是数字n各个位子上的数字和,L就是数字n中,【5~9】的数字的个数。
接下来贪心的从后往前设定值即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int ans[150000];
int gcd(int x,int y)
{
if(y==0)return x;
else return gcd(y,x%y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
int s=9*b;
int l=2*b-a;
if(l==0){printf ("1\n"); continue;}
if(l<0){printf ("0\n"); continue;}
if(5*l>s){printf("0\n");continue;}
int gc=gcd(s,l);
l/=gc;
s/=gc;
s-=5*l;
int cnt=0;
for(int i=0;i<l;i++)
{
if(s>0)
{
int ad=min(4,s);
s-=ad;
ans[cnt++]=5+ad;
}
else ans[cnt++]=5;
}
while(s>0)
{
if(s>0)
{
ans[cnt++]=min(s,4);
s-=min(s,4);
}
else break;
}
for(int i=cnt-1;i>=0;i--)
{
printf("%d",ans[i]);
}
printf("\n");
}
}