Codeforces Round #553 Div.2 C - Problem for Nazar

模拟+数学

摆放的数是按2的幂增长的,所以直接模拟应该是O(logn)。。

我们直接模拟统计奇数和偶数的个数就行了,用前缀和相减可以得到答案。

前缀和直接用等比数列求和。。然而我竟然忘了QAQ,小学生都不如了。。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}

const int mod = 1e9 + 7;
ll solve(ll n){
    int flip = 1;
    ll odd = 0, even = 0, cur = 1, sum = 0;
    while(1){
        if(sum + cur > n) break;
        flip ? odd += cur : even += cur;
        sum += cur, flip ^= 1, cur <<= 1;
    }
    flip ? odd += n - sum : even += n - sum;
    return (((odd % mod) * (odd % mod)) % mod + ((even % mod) * ((even + 1) % mod)) % mod) % mod;
}

int main(){

    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll l, r;
    cin >> l >> r;
    cout << ((solve(r) - solve(l - 1)) % mod + mod) % mod << endl;
    return 0;
}
posted @ 2019-05-07 22:06  清楚少女ひなこ  阅读(122)  评论(0编辑  收藏  举报