hdu 4588 Count The Carries

1054 ワード

数論、法則を探す
でもなぜg++提出はwaで、c++でいいの?
テーマの中で最も重要な法則は1つの数x(10)=yyyyに対して....(2),1からx,y[i]における1と0の交互の周波数はiの変化に伴って変化する.
#include <cstdio>
#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;
typedef __int64 ll;
ll n, m;
ll cal(ll x, int i, ll bg )
{
    if (x < bg) return 0;
    ll rg = ll(2<<i);
    ll tp = x-bg+1;
    ll a = tp/rg, b = tp%rg;
    rg >>= 1;
    return a*rg+min(b,rg);
}
ll solve(ll y, ll x)
{
    if (x == y ) return 0;
    ll bg = ll(1), res = 0, cy = 0;
    for (int i = 0; bg <= x; ++i, bg <<= 1)
    {
        cy += cal(x,i,bg)-cal(y-1,i,bg);
        cy >>= 1; res += cy;
    }
    while (cy)
    {
        cy >>= 1; res += cy;
    }
    return res;
}
int main() 
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%I64d%I64d", &n, &m) != EOF)
    {
        printf("%I64d
", solve(n, m)); } return 0; }