C. Problem for Nazar

在这里插入图片描述
题解:
At first let’s simplify the problem. Let’s denote as f(x) function that returns sum of the elements that are on positions from 1 to x inclusive.
How to implement function f(x)?
To find the answer we can find the sum of even and sum of odd numbers and add them. Let’s find how many there are even and odd numbers. Let’s call them cureven and curodd. Iterate through the powers of two. If current power is even, then add minimum from x and current power of two to curodd, otherwise add minimum from x and current power of two to cureven, after that reduce x by current power of two. If x becomes less than or equal to 0 break.
Now our task is reduced to twos: for given number n find the sum of first n even numbers and for given number m find the sum of first m odd numbers. The answer for first task is n⋅(n+1). The answer for second task is m2.
Now the answer for the problem is f®−f(l−1).
Don’t forget about modulo
Complexity is O(logN).

#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<map>
#include<iterator>
#include<queue>
#include<vector>
#include<string>
using namespace std;

typedef long long ll;
const int N=1e6+10;
const long long INF=1e18;
const double eps=0.0000001;
const ll mod=1e9+7;
int a[600][600];
int vis[600][2];
vector<int>cup;

ll f(ll x)
{
    ll odd=0,even=0,sum=0,num=1;
    while(x-num>=0)
    {
        x-=num;
        if(sum%2==0)
            odd=(odd+num)%mod;
        else
            even=(even+num)%mod;
        num*=2;
        sum++;
    }
    if(sum%2==0)
        odd=(odd+x)%mod;
    else
        even=(even+x)%mod;
    return ((odd*odd)%mod+(even*even)%mod+even)%mod;
}

int main()
{
   ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   ll n,m;
   cin>>n>>m;
   cout<<(f(m)-f(n-1)+mod)%mod;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值