http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2609
A-number and B-number Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title Description
Tom is very interested in number problem. Nowadays he is thinking of a problem about A-number and B-number.
A-number is a positive an integer whose decimal form contains 7 or it can be divided by 7. We can write down the first ten a-number (A[i] is the ith a-number)
{a[1]=7,a[2]=14,a[3]=17,a[4]=21,a[5]=27,a[6]=28,a[7]=35,a[8]=37,a[9]=42,a[10]=47};
B-number is sub-sequence of A-number which contains all A-number and A[k] (that's K is A a-number.) Like, the 7th A-number and 7 are also an a-number so the (A[7]) are not A b-number. We also can write down the first ten b-number.
{b[1]=7,b[2]=14,b[3]=17,b[4]=21,b[5]=27,b[6]=28,b[7]=37,b[8]=42,b[9]=47,b[10]=49};
Now Given a integer N, please output the Nth B-number Enter the input consists of multiple test cases.
For each test case, there would be a positive integer N as the description.
Output
For each test case, the output of an integer indicating the Nth b-number.
You can assume the result would be is no more then 2^63-1.
Sample input
17100
Sample output
737470
Hint Source 2013 The first ACM University student Program design contest in Shandong Province sample program
Analysis:
At first thought of playing the table, the result time-out, and later looked at the standard process with a search written,,, Orz
Timeout code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 BOOLFunintN) {6 if(n%7==0)return true;7 while(n) {8 if(n%Ten==7)return true;9N/=Ten;Ten } One return false; A } - Long Longa[100000010]; - Long Longb[100010000]; the intMain () { - intI,c=0, cc=0; - //freopen ("In.txt", "R", stdin); - for(i=1; i<=100000000; i++) + if(Fun (i)) a[++c]=i; - for(i=1; i<c;i++) + if(!Fun (i)) A{b[++cc]=a[i];} at Long LongN; - while(cin>>N) { - if(n>cc-1) cout<<"??"; - Else -cout<<b[n]<<Endl;} - return 0; in}View Code
Official Standard Range:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineULL unsigned long Long6 ConstULL maxn= (1ull<<64ull)-1);7ULL dp[ A][Ten][2];8 intdigit[ -];9ULL DFS (intPosintPreintFlagBOOLlimit) {Ten if(pos==-1)returnflag| | pre==0; One if(!limit&&dp[pos][pre][flag]! =-1)returnDp[pos][pre][flag]; A intEnd = Limit?digit[pos]:9; - intFflag,ppre; -ULL ans=0; the for(inti =0; I <= end;i++){ -Fflag = (i==7)||Flag; -Ppre= (pre*Ten+i)%7; -Ans+=dfs (pos-1, ppre,fflag,limit&&i==end); + } - if(!limit) dp[pos][pre][flag]=ans; + returnans; A } atULL Solve (ULL N) {//find a few a-number below N - intpos =0; - while(n) { -digit[pos++] = n%Ten; -N/=Ten; - if(!n) Break; in } - returnDFS (pos-1,0,0,1)-1; to } +ULL Find (ULL N) {//find a few b-number below N -ULL t = Solve (n);//indicates that n includes n or less in a with T theULL tt = t-solve (t);//Indicates that a few of the A_number in T (including the T) are in accordance with B, and have been found that the TT is exactly equal to n * returntt; $ }Panax Notoginseng intMain () - { thememset (dp,-1,sizeof(DP)); + ULL N; A while(cin>>N) { theULL L =0, r=Maxn,mid; + while(L <=R) { -Mid = ((r-l) >>1)+l; $ if(Find (mid) <n) L = mid+1; $ Elser = mid-1; - } -ULL ans = r+1; thecout<<ans<<Endl; - }Wuyi return 0; the}View Code
Sdutoj 2609 A-number and B-number