The fourth session of Shandong Province A-number and B-number [digital dp+ Two-point answer] dynamic planning

Source: Internet
Author: User
Tags arrays time limit

Topic Link: Hrbust's OJ on the topic

————————————————————————————————
A-number and B-number
Time limit:1000 MS Memory limit:32768 K
Total submit:22 (8 users) Total Accepted:5 (5 users) rating:special Judge:no
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.

Input
The input consists of multiple test cases.

For each test case, there would be a positive integer N as the description. (0<n<263−1) (0

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
1
7
100
Sample Output
7
37
470

——————————————————————————————————

Main topic:
A array is a series of folk games "knock Seven"
B array is {x∣∣xi∈{a} and I∉{a}} \{x \big| x_i \in \{a\} and I\notin \{a\}\}
Then output the nth element in the B array: Bn b_n

Problem Solving Ideas:

If I were to find the first element I would not be able to ask whether it was an array of a or B (I would not)

But he's got the I-I element. We can think of it in a different way.

First we can use an entry-level digital DP to count the number of elements less than n N,

Then the smallest n of the x element in the [1,n] is the first X element

Then we can use the two-point answer to count.

For a arrays we're good at digital DP
But we can't just ask for a B-array.

However, considering the relationship between A and B arrays, it is found that as long as the results of CAL (X) Statistics ans is statistically subtracted;

That

A = cal (x);
B = a-cal (A);

Attention
Two-point overflow problem
Maximum Value (1<<63)-1,

Attached code
——————————————————————————————————————————————

#include <bits/stdc++.h> using namespace std;
typedef long long int LL;
/********************************/int num[20];

LL dp[20][8][2]; LL dfs (int pos,int mod,int limit,int status) {if (pos<0) return (status| |
    mod%7==0); if (Dp[pos][mod][status]!=-1&&!limit) return dp[pos][mod][status];
    Forget to write limit Meng forced 1000 days there is no int endi=9;

    if (limit) endi=num[pos];
    LL res = 0; for (int i=0;i<=endi;i++) Res+=dfs (pos-1, (mod*10+i)%7,limit&&i==endi,i==7| |

    status);
    if (!limit) dp[pos][mod][status] = res;
return res;
    } ll Cal (ll x) {if (!x) return 0;
    int len=0;
    while (x) num[len++]=x%10,x/=10;
Return DFS (len-1,0,1,0)-1;  } void Solve (ll x) {ll l=7,r= (1ll<<63) -1,mid,ans=-1;
        R written in 1e18 WA Meng has no,,,, while (l<=r) {mid= ((r-l) >>1) +l;
        LL tmp = CAL (mid);
        TMP-= CAL (TMP);
        if (tmp>=x) ans=mid,r=mid-1;
    else l=mid+1;
} printf ("%lld\n", ans); } int main () {///printf ("%lld\n ", ~ (1ll<<63));
    Memset (Dp,-1,sizeof (DP));
    LL N;
    while (~SCANF ("%lld", &n)) solve (n);
return 0; }

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.