A-number and B-number
Time limit:1000ms Memory limit:65536k in doubt. 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 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 are sub-sequence of A-number which contains all a-number but A[k] (that's K is a a-number. ) Like, are 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
Input The input consists of M Ultiple 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
Tips
Source2013 ACM College Student Program Design contest in Shandong Province
Sample ProgramDigital DP processing a array of number two to find the ID of the X B array as an array of IDs in binary find the value of the corresponding a array to use ull or it will be wrong accode
#include <iostream> #include <cstdio> #include <cstring> #define LL unsigned long long #define MID (l+r
) >>1) using namespace std;
ll Dp[19][2][7];
int data[19]; inline ll dfs (int len,bool is_7,int mod_7,int limit) {if (!len) return (is_7| |
(mod_7==0));
if (!limit&&dp[len][is_7][mod_7]!=-1) return dp[len][is_7][mod_7];
int ed=limit?data[len]:9;
ll Ans=0; for (int i=0;i<=ed;++i) Ans+=dfs (len-1,is_7| |
I==7, (mod_7*10+i)%7,limit&&i==ed);
return Limit?ans:dp[len][is_7][mod_7]=ans;
} Inline ll Fun (ll N) {int len=0;
while (n) {data[++len]=n%10;
n/=10;
} return Dfs (len,0,0,1)-1;
} void doit (ll n) {ll l=n,r= (long Long) 1<<63);
ll add;
while (L<r) {ll tmp=mid-fun (mid);
if (tmp==n) {for (ll k=l;k<=mid;++k) if (K-fun (k) ==n) {add=fun (k);
Break
} break; } IF (tmp>n) R=mid;
else L=mid;
} l=0,r= ((Long Long) 1<<63);
N+=add;
while (L<r) {ll tmp=fun (mid);
if (tmp==n) {for (ll k=l;k<=mid;++k) if (Fun (k) ==tmp) {cout<<k<< ' \12 ';
Return
}} if (Tmp<n) L=mid;
else R=mid;
}} int main () {ll n;memset (dp,-1,sizeof (DP));
Ios::sync_with_stdio (FALSE);
while (cin>>n) doit (n);
return 0;
}