link:http://acm.timus.ru/problem.aspx?space=1&num=1057
论文: 浅谈数位类统计问题 刘聪
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cctype> #include <algorithm> #include <queue> #include <deque> #include <queue> #include <list> #include <map> #include <set> #include <vector> #include <utility> #include <functional> #include <fstream> #include <iomanip> #include <sstream> #include <numeric> #include <cassert> #include <ctime> #include <iterator> const int INF = 0x3f3f3f3f; ][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}}; using namespace std; ][]; int bto2(int x, int b) { ], cnt = , ans = ; while (x) { tm[cnt++] = x % b; x /= b; } ; i >= ; --i) { )) { ans += ((<<(i+))-); break; } <<i); } return ans; } int calc(int x, int k) { , ans = ; ; i > ; --i) { <<i)) { ++tot; if (tot > k) break; x = x ^ (<<i); } <<(i - )) <= x) ans += (a[i-][k-tot]); } if (tot + x == k) ++ans; return ans; } int main(void) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin ); #endif // ONLINE_JUDGE int x,y,k,b; memset(a, , sizeof(a)); a[][] = ; ; i <= ; ++i) { a[i][] = a[i-][]; ; j <= i; ++j) a[i][j] = a[i-][j] + a[i-][j-]; } while (~scanf("%d%d%d%d",&x, &y, &k, &b)) { ; ) { ans = calc(y, k) - calc(x - , k); } else { , b), Y = bto2(y, b); ans = calc(Y, k) - calc(X, k); } printf("%lld\n", ans); } ; }
走吧,小胖!