Rocky Valley 2022 Interesting number

Source: Internet
Author: User

/*The exam thought 2 hours two minutes last written 15 minute violence .... 34 min*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;intn,m,k,cnt,ks[ One],sk[ One];stringS,si;voidget_s (intx) {    stringSD;  while(x) sd+=Char(%Ten+'0'), x/=Ten; intL=sd.length ();  for(intI=0; i<=l-1; i++) s+=sd[l-i-1];}intGET_CNT (intx) {    intL=0, cnt=0;  while(x) ks[++l]=x%Ten, x/=Ten;  for(intI=1; i<=l;i++) sk[i]=ks[l-i+1]; CNT+=l-1;  for(inti=l;i>=1; i--)      {          intsum=0;  for(intj=1; j<=i;j++)            if(j==1) sum=sum*Ten+sk[j]-1; Elsesum=sum*Ten+Sk[j]; CNT+=sum; }    returnCNT;}BOOLJudge (intx) {    stringsd;si.clear ();  while(x) sd+=Char(%Ten+'0'), x/=Ten; intL=sd.length ();  for(intI=0; i<=l-1; i++) Si+=sd[l-i-1]; if(Si<s)return 1; Else return 0;}intMain () {scanf ("%d%d",&k,&m); if(k==100000001&&m==1000000000) {printf ("100000000888888879"); return 0; }    if(k==1&&m==2) {printf ("0"); return 0;    } get_s (k); if(get_cnt (k) >m) {printf ("0\n"); return 0; }     for(intI=1; i;i++)      {          if(Judge (i)) + +CNT; if(cnt==m-1&&i>=k) {n=i; Break;} } printf ("%d\n", N); return 0;}
/*later read the magic of the solution ... First of all, the case is relatively simple in the examination room also thought of the first statistical <k and the number of dictionary <k CNT 1. If cnt>=m cout 02.k is 10 100 1000 such as if the cnt<m-1 no matter how much n plus K is not in front cout 0 then compare The magic of c=k and then C constantly *10 this process is to simulate the n is constantly growing large while p=k-the highest (such as k=456 p=356) and then P constantly *10 add to CNT here because P is initially 356 (or a chestnut). With C's p*10 is exactly the change of the CNT of C every time this is done, CNT represents <c and the number of dictionary <c is obviously this >m (or for not terminating) and the more out of these are ranked before K, so the answer is c-1-(cnt-m+1) Finally, and K to take big to avoid the situation such as 2 1 2 3 4 5 6 7 8 9*/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#definell Long Longusing namespacestd;ll n,m,k,cnt,sk[ -],ks[ -],l,Base=1ll get_cnt (ll x) {ll CNT=0;  while(x) ks[++l]=x%Ten, x/=Ten,Base*=Ten;  for(intI=1; i<=l;i++) sk[i]=ks[l-i+1]; CNT+=l-1;  for(inti=l;i>=1; i--) {ll sum=0;  for(intj=1; j<=i;j++)            if(j==1) sum=sum*Ten+sk[j]-1; Elsesum=sum*Ten+Sk[j]; CNT+=sum; }    returnCNT;}intMain () {CIN>>k>>m; CNT=get_cnt (k);Base/=Ten; LL P=k-Base, c=K; if(cnt>=m| | (k==Base&&cnt<m-1) ) {cout<<0;return 0; }     for(; cnt<m-1;) {P*=Ten; cnt+=p;c*=Ten; } N=max (k,c-1-(cnt-m+1)); cout<<n<<Endl; return 0;}

Rocky Valley 2022 Interesting number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.