Poj3252 combined mathematics

Source: Internet
Author: User

Poj3252

Repeat the problem:

Number of round numbers in the range [start, finish. The so-called round number indicates the integer in which the number of digits of 0 is greater than or equal to 1.

Analysis:

1. assume that the number of round numbers in the [0, finish] and [0, start-1] ranges is N1, N2, the answer to the question is N1-N2. therefore, the question can be converted to the number of round numbers smaller than N.

2. If the binary value of integer N is 1, N2, N3, N4,... NK, the number of roundnumbers smaller than N can be divided into two parts.

1) binary indicates the number of round numbers whose digits are smaller than K.

It is easy to know that the number of round numbers with the number of digits J is C [J-1] [0] + C [J-1] [1] +... + C [J-1] [J/2-1].

2) number of round numbers whose digits are K and less than N.

M less than N can be understood as: M and N binary indicate that all bits are the same before the first different bits appear, and mj = 0, nj = 1, (J indicates the sequence number of the first different bits ). Therefore, the number of round numbers smaller than N can be calculated using the following algorithm: Find N non-zero point nj = 1, make mj = 0, and M1 = N1, M2 = n2 ,.., MJ-1 = nj-1, so you only need to make the number of non-zero digits of the remaining K-J bit so that M meets the condition of the round number.

For example, if n = 1010010, 1) makes M = 100 XXXX, then the last four digits of M can only have 2 1, therefore, C [4] [0] + C [4] [1] + C [4] + C [4] [2]; 2) makes M = 101000x, then the number of M that meet the condition is C [1] [0] + C [1] [1].

The problem can be solved based on the above analysis.

AC code

 1 //Memory: 232K        Time: 16MS 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5  6 using namespace std; 7  8 int c[64][64]; 9 int n[64];10 11 void init_c()12 {13     for (int i = 0; i < 64; i++) {14         c[i][0] = 1;15         c[i][i] = 1;16     }17     for (int i = 2; i < 64; i++)18         for (int j = 1; j < i; j++)19             c[i][j] = c[i - 1][j] + c[i - 1][j - 1];20 }21 22 int a[40];23 int len = 0;24 25 int roundNum (long long n)26 {27     len = 0;28     while(n) {29         a[len++] = n % 2;30         n /= 2;31     }32     int ret = 0;33     34     for (int i = 2; i < len; i++) {35         for (int j = 0; j <= i / 2 - 1; j++) {36             ret += c[i - 1][j];37         }38     }39 40     int cnt = 1;41     for (int i = len - 2; i >= 0; i--) {42         if (cnt > len / 2) break;43         if ( a[i] ) {44             for (int j = 0; j <= len / 2 - cnt; j++)45                 ret += c[i][j];46             cnt++;47         }48     }49 50     return ret;51 }52 53 int main()54 {55     init_c();56     long long s, f;57     cin >> s >> f;58     cout << roundNum(f + 1) - roundNum(s) << endl;59     return 0;60 }

 

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.