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 ;}