hdu 4588 Count The Carries

数论,找规律

不过为什么g++提交就是wa,c++就可以

题目中最重要的规律就是对于一个数 x(10) = yyyyy....(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\n", solve(n, m));
    }
    return 0;
}


你可能感兴趣的:(hdu 4588 Count The Carries)