Hit summer training contest 11/A Step number

Source: Internet
Author: User

Directory

Question

I don't know why I thought about it for a long time. N <= 2 ^ 31 times out.

After the competition, I knocked on it,ProgramThe efficiency is very high. Nima only reflected that this is bitwise,Search for every 10 digits... Nima super ghost ....It should be said that the search isSpecial answer treeTo the left, expand the 0-9 subnode as much as possible. The larger the number is, the smaller the number is to the right. When a = 1 B = 999999999 is input, only 43758 queries are searched.

Reflection: I remember that when I read Liu rujia's White Book, he mentioned this situation. I will analyze it carefully in the future. Don't be afraid to do it too much. I 've done it several times...

ThisCodeMessy writing

# Include <cstdio> # include <cstring> # include <iostream> using namespace STD; int sum; void solve (INT islim, int num [], int premax, int cur, int N, int ans) {int curlim; If (islim) curlim = num [cur]; else curlim = 9; If (cur = N-1) {sum + = (curlim-premax + 1); // printf ("% d \ n", ANS, (curlim-premax + 1 ));} else {If (curlim> = premax) {for (INT pre = premax; Pre <= curlim; Pre ++) solve (islim & (pre = curlim )), num, pre, cur + 1, n, NS * 10 + pre) ;}}int getnum (int x, int num []) {int Buf [20]; int I; for (I = 0; X; I ++) {Buf [I] = x % 10; // 456 654x/= 10;} For (Int J = 0; j <I; j ++) {num [J] = Buf [i-j-1];} return I;} int main () {int A, B; // 1 <31 = 2147483648 where there is a 1, so there is no impact. int32 can also be int num [20]; while (scanf ("% d", & A, & B )! = EOF) {if (a <0) A = (1 <31)-1; if (B <0) B = (1 <31)-1; int n = getnum (a-(A = 1? 0: 1), num); solve (1, num, 0, 0, N, 0); int ans = sum; n = getnum (B, num); sum = 0; solve (1, num, 0, 0, N, 0); ans = sum-ans; if (a = 1) ans + = 1; printf ("% d \ n", ANS); sum = 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.