HDU-4321-Arcane numbers 2-digital statistics

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4321

Given a, B, and N, let you calculate the number 1 in B + A, B + 2 * a,... B + N *.

Question:

This question was read after too many schools. I have been reading it for a long time and haven't understood it for a long time. Now we can understand it in about 20 minutes. I used to be absent-minded?

Calculate by bit. If enumeration is used, the complexity is 64 * n.
Article 1
When calculating the K bit, we can note that the B + T * A and B + (t + 2 ^ (k + 1) * A bit is the same.
Then, you only need to count 2 ^ (k + 1)-1 times for the K-bit.

Article 2
When calculating the K bit, we can note that the continuous (2 ^ K)/A is continuous 0 or continuous 1. Therefore, we can consider direct continuous records (2 ^ K) /A results.

Then, you only need to count N/(2 ^ K)/A) times at the K-bit.

Based on the first article and the second article, you only need to calculate the complexity of 2 ^ K/(2 ^ K)/A) to O ()

The total complexity is 64 *

# Include <cstdio> # include <cstring> int main () {int t; _ int64 A, B, n; scanf ("% d", & T ); for (int cas = 1; CAS <= T; ++ CAS) {scanf ("% i64d % i64d % i64d", & A, & B, & N ); _ int64 CNT = 0; _ int64 max = B + A * n; For (_ int64 I = 0; I <64; ++ I) {// one-bit statistic _ int64 M = 1ll <I; _ int 64mm = m; If (M> MAX) break; m <= 1; _ int64 cur = a + B; _ int64 J = 0; while (j <M & J <n) {_ int64 upper = cur + (Mm-(cur % mm)-1ll; _ int64 step = (upper-cur)/A + 1ll; if (J + Step> = N) {step = N-J;} If (J + Step> = m) {step = m-J ;} if (cur & (1ll <I) {CNT + = Step * (N/m); If (J + step <(N % m )) {CNT + = step;} else if (j <(N % m) {CNT + = (N % m)-J ;}} cur + = A * step; J + = Step ;}} printf ("case # % d: % i64d \ n", Cas, CNT) ;}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.