Njust 1739-count the carries

Source: Internet
Author: User


I didn't make it during the competition. I 'd like to discuss with you the number of statistics I. I want to calculate the first 0 to the A-1 carry, the number and then calculate the number of 0-B into the number of digits and then the latter minus the former is, test the error, changed to 0 to a is still wrong, it seems that this idea is incorrect. I still honestly counted the number of 1 in [a, B. That's it. When the statistics I am each bit of the statistical, and then 0 to B minus the number of 1, 0 to the number of 1 of the A-1.

When counting the number of 1. I am using a packaging idea. The last one is the alternating variation of 0 and 1, and the former one is the variation of 0, 0, and 1, so that 2 ^ n (n = 0.1.2.3 ....) the same number, such as 1 or 0 respectively, is regarded as a 0 or 1 packet, so the number of statistical packets can be solved recursively, And I implement it in a loop.


# Include <stdio. h> // long int num (int A, int B) // {// miserable Code // Long ans = 0; // int p_tmp = 0; // int packet = 1; // int carri_tmp = 0, one_num_tmp = 0; // while (A> = packet) {// p_tmp = (a + 1)/packet; // one_num_tmp = 0; /// if (p_tmp % 2 = 1) // one_num_tmp = a + 1-p_tmp * packet; //// p_tmp = p_tmp/2 // one_num_tmp + = (p_tmp/2) * packet + carri_tmp; // carri_tmp = one_num_tmp/2; /// ans + = carri_tmp; /// packet * = 2; //} // while (carri_tmp> 1) // {// carri_tmp/= 2; // ans + = carri_tmp; //} // return ans; //} long int one (long int A, long int packet_base) {// 0... a-1 long int packet = A/packet_base; long int ans = 0; If (packet % 2) ans + = A-packet * packet_base; ans + = packet/2 * packet_base; return ans;} long int num (long int A, long int B) {long int packet_base = 1; long long int one_num_a, one_num_ B, one_num; long Int carri = 0, ANS = 0; while (packet_base <= B) {one_num_a = 0; If (packet_base <= A) {one_num_a = one (A, packet_base );} one_num_ B = one (B + 1, packet_base); one_num = one_num_ B-one_num_a + carri; carri = one_num/2; ans + = carri; packet_base * = 2 ;} while (carri> 1) {carri/= 2; ans + = carri;} return ans;} int main () {long int A, B; while (~ Scanf ("% LLD", & A, & B) // while (scanf ("% d", & A),) {printf ("% LLD \ n", num (a, B) ;}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.