Digit-Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 848 Accepted Submission(s): 260
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
Source
Recommend
#include<iostream> #include<algorithm> #include<string> #include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0}; #include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;} #include<vector> #include<cmath> #include<stack> #include<string.h> #include<stdlib.h> #include<cstdio> #define maxn 150003 #define ll long long #define INF 10000000 using namespace std; int a,b; int _gcd(int x,int y) { if(y==0) return x; return _gcd(y,x%y); } /* 典型的数学规律题(我居然有底气说典型) 好吧,,思路还是参考的。 题目大意:对于一个数定义一个函数返回值为其每一位的权重和。 求满足等式a*S(n)=b*S(2*n)的最小n; 打表找规律先,找出S(2*n)和S(n)的关系。。 先考虑其影响因子。。考虑到2为系数,所以大于5的数为一种影响因子。 S(2*n)=S(n)-9*L;其中L为n中大于4的数的个数 于是乎下面就是各种数学化简,对于最后的等式, 可以贪心一点,让S,L分别为其系数即可,, 然后用最大公因数预先处理一下。 对于最后得到的S,L贪心构造。 详情见代码。。。。 下面详解规律的由来:(不找出来都对不住我以前学的数学啊啊啊啊啊啊) S(X1 X2 X3 X4...Xn*2) =S(X1*2*10^(n-1)+...Xn*2*10^0) 因为对于每个xi其位上的权重已经被区分开来所以函数可拆分 =S(2*x1)+S(2*x2)+...S(2*xn) 这样就归结到对于个位数时函数的规律了。。。 那么简单的个位数的话直接打表不难找到规律,只要引入一种函数即G(n)=n>=5?1:0; 最后总函数的影响便是每个位数的G函数相加即可,,即为L。 */ int main() { int t;scanf("%d",&t); while(t--) { scanf("%d%d",&a,&b); int tp=2*b-a; if(tp<0) { puts("0"); continue; } if(tp==0) { puts("1"); continue; } int S=9*b,L=2*b-a; if(5*L>S) { puts("0"); continue; } int gcd=_gcd(S,L); S/=gcd,L/=gcd; int ans[maxn],cnt=0; while(cnt!=L) ans[cnt++]=5; S-=5*L; for(int i=0;i<L;i++) { if(S>0) { int sd=min(S,4); ans[i]+=sd; S-=sd; } } if(S>0) { int sd=S/4; while(sd--) ans[cnt++]=4; ans[cnt++]=S%4; } for(int i=cnt-1;i>=0;i--) printf("%d",ans[i]);//wrong的原因竟然是。。。顺序问题?! puts(""); } return 0; }