Test instructions
Defines s (n) as the number n and the number of digits on each bit.
In the given two number, a, the smallest positive integer n, making AXS (n) =bxs (2n).
Official:
这道题目的结果可能非常大,所以我们直接枚举n是要GG的。首先可以有这样的基础性结论:设gcd(a,b)=g, 我们可以先使得b=b/g, a=a/gS(n):S(2n)==b:a,那么我们有S(n):S(2n)=b:a。然后,一个好的做法是,我们研究本质问题。我们发现,如果一个digit是0~4,那么*2的效益是完全获得的。如果一个digit的是5~9,那么*2后会损失9的收益。a*S(n) == b*S(2n),我们假设有l的长度是[0,4]范围,有L的长度是[5,9]范围那么显然满足:S(2n)=S(n)*2-L*9替换一下——a*S(n) == b*(2S(n)-L*9)a*S(n) == 2b*S(n) -L*9*b(2b-a)*S(n) == L*9*b即——9*b:2b-a = S(n):L也就是说,我们得到了S(n)与L的比例关系。然后模拟一遍即可。怎么模拟呢?我们不妨假设答案n仅有长度为L,且每一位都是5然后得到了把数位和sum分撒出去。对于sum余下的数值,我们依次加到尾巴上。如果sum最后把长度为L的字串都填充为‘9‘之后,还有剩余,那么在前面贪心填充。
Structural problems are generally found in law. If you find it, it suddenly dawned on me. I'm so this question!
Be bold and careful with your questions.
Code:
#include <iostream>using namespacestd;//a*s (n) =b*s (2n)//a*s (n) =b* (2*s (n) -9*l)//(a-b*2) *s (n) =-b*9*l//(b*2-a)/b*9=l/s (n)intgcdintAintb) {returnB? GCD (b, a%b): A; }intans[1005];intMain () {intT; CIN>>T; while(t--) { intA, B; CIN>> a >>b; intL = b *2-A; intsn = b *9; if(5* L > SN | | L <0) {cout<<"0"<<Endl; Continue; } if(L = =0) {cout<<"1"<<Endl; Continue; } intGG =gcd (L, SN); L/= GG; SN/=GG; intIDX =0; SN-=5*l; for(inti =0; I < L; ++i) {intadd = Min (4, SN); SN-=add; Ans[idx++] =5+add; } while(SN) {intres = min (4, SN); Ans[idx++] =Res; SN-=Res; } for(inti = idx-1; I >=0; I.) cout <<Ans[i]; cout<<Endl; } return 0;}
HDU 5710 digit-sum (construction)