Digit-Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 784 Accepted Submission(s): 242
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
题解:
对于每一位x,有
1:x<5,则2*S(x)=S(2*x)
2: x>5, 则2*S(x)-9=S(2*x)
因为逢十进一损失了9,因此减九。
设m中有L位>=5,则推下去有: S(2*n)=2*S(n)-9*L
因此有(9*b*L)/(2*b-a)=S(n)
不难得出,第一个使得(9*b*L)/(2*b-a)为整数的L便是最优解。
说到这里不禁要感谢天才的小巨轮手搓600个数得出了这一精辟结论
#include<bits/stdc++.h> using namespace std; int k[14]; int gcd(int a,int b) { if(b==0)return a; return gcd(b,a%b); } int main() { int T;scanf("%d",&T); while(T--) { memset(k,0,sizeof(k)); int a,b; scanf("%d%d",&a,&b); if(2*b-a<0)k[0]=1; else if(2*b==a) k[1]=1; else { int g=gcd(9*b,2*b-a); int sum=9*b/g,L=(2*b-a)/g; if(5*L>sum) k[0]=1; else if(9*L>=sum) { for(int i=0; ;i++) { if(5*i+9*(L-i)<=sum) { if(5*i+9*(L-i)==sum) k[5]=i,k[9]=L-i; else { int x=sum-5*(i-1)-9*(L-i); k[5]=i-1;k[x]=1;k[9]=L-i; } break; } } } else { sum-=9*L; for(int i=0; ;i++) { if(4*(i+1)>=sum) { int x=sum-4*i; k[x]=1;k[4]=i;k[9]=L; break; } } } } for(int i=0;i<10;i++) { while(k[i]--) printf("%d",i); } printf("\n"); } return 0; }