Codeforces Round #553 (Div. 2) C

本文解析了Codeforces上一道关于无限数列的数学难题,通过巧妙地将问题转化为区间和计算,使用C++实现了一个高效的算法解决方案。该算法能够处理高达10^18的输入范围,并正确返回模10^9+7后的结果。
C. Problem for Nazar
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.

Consider two infinite sets of numbers. The first set consists of odd positive numbers (1,3,5,7,), and the second set consists of even positive numbers (2,4,6,8,). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another.

The ten first written numbers: 1,2,4,3,5,7,9,6,8,10. Let's number the numbers written, starting with one.

The task is to find the sum of numbers with numbers from ll to rr for given integers ll and rr. The answer may be big, so you need to find the remainder of the division by 1000000007 (109+7).

Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.

Input

The first line contains two integers ll and rr (1lr1018) — the range in which you need to find the sum.

Output

Print a single integer — the answer modulo 1000000007 (109+7).

Examples
input
1 3
output
7
input
5 14
output
105
input
88005553535 99999999999
output
761141116
Note

In the first example, the answer is the sum of the first three numbers written out (1+2+4=7).

In the second example, the numbers with numbers from 5 to 145,7,9,6,8,10,12,14,16,18. Their sum is 105.

 

链接: http://codeforces.com/contest/1151/problem/C

 

赛后补题。

 

题意:给你一个无穷的数列 数列的第一部分长度为 1 ,由(1)组成、第二部分长度是2,由 (2 4) 组成、第三部分长度为4 由(3 5 7 9)组成,接下来的还是同样的规律:长度*2 奇偶互换。 求[l,r]这个区间的区间和。

   常规的区间操作 【l,r】 = 【1,r】- 【1,l-1】

   枚举每个子区间,统计出来奇数和偶数的个数,然后就是等差数列求和。

  

#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long

const ll mod = 1e9+7;
ll solve(ll x){
    ll cnt[2] = {0,0};
    int k = 0;// 记录奇偶位
    ll c = 1; // 记录区间的长度
    while(x){
        cnt[k] += min(c,x);
        // 当最后的那个区间长度小于 2^(n-1) (n是区间的个数) 时
        // 才会 cnt[k] += x;
        x -= min(c,x);
        k^=1;
        c <<= 1;
    }
    ll sum = (cnt[0]%mod)*(cnt[0]%mod)%mod;
    sum += (cnt[1]%mod)*((cnt[1]+1)%mod)%mod;
    return sum%mod;
}

int main(){
    ll l, r;
    scanf("%I64d%I64d",&l,&r);
    printf("%I64d\n",(solve(r)-solve(l-1)+mod)%mod);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/kongbb/p/10757596.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值