C. Problem for Nazar

52 篇文章 1 订阅

https://codeforces.com/problemset/problem/1151/C

题目描述

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, \ldots1,3,5,7,… ), and the second set consists of even positive numbers ( 2, 4, 6, 8, \ldots2,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, 101,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 10000000071000000007 ( 10^9+7109+7 ).

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

输入格式

The first line contains two integers ll and rr ( 1 \leq l \leq r \leq 10^{18}1≤l≤r≤1018 ) — the range in which you need to find the sum.

输出格式

Print a single integer — the answer modulo 10000000071000000007 ( 10^9+7109+7 ).

题意翻译

设正奇数集合为\mathrm{A}A,正偶数集合为\mathrm{B}B,这两个集合是无限集。

在黑板上写了无数轮数,第ii轮写下了2^{(i-1)}2(i−1)个数.

当ii为奇数时,从集合\mathrm{A}A中向后取数,当ii为偶数时,从集合\mathrm{B}B中向后取数。

求黑板上第ll个数到第rr个数的和,模\mathrm{1000000007}1000000007(10^9+7109+7)。

1 \le l,r \le 10^{18}1≤l,r≤1018

输入输出样例

输入 #1复制

1 3

输出 #1复制

7

输入 #2复制

5 14

输出 #2复制

105

输入 #3复制

88005553535 99999999999

输出 #3复制

761141116

说明/提示

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

In the second example, the numbers with numbers from 55 to 1414 : 5, 7, 9, 6, 8, 10, 12, 14, 16, 185,7,9,6,8,10,12,14,16,18 . Their sum is 105105 .


思路:出现了l~r之间的和,转化成用前缀和r~l-1的和

首先考虑到1e18最多会到第几层。

void init()
{
	a[1]=1;sum[1]=1;
	for(LL i=2; ;i++)
	{
		a[i]=a[i-1]*2;
		sum[i]=sum[i-1]+a[i];cnt++;
		if(sum[i]>=1e18) break;
	}
}

输出知道60层就可以了。所以这里的就是个常数次累加。

然后要算1-x的和是多少。由于不是连续的等差,由题目条件是分成奇数和偶数的等差。由等差公式可以得出

奇数Sn=n^2;偶数Sn=n^2+n;

所以直接枚举当前x前有多少个偶数个数和奇数个数,就可以方便并且O(1)最后算出1-x的前缀和。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=80;
typedef long long LL;
typedef unsigned long long ull;
const LL mod=1e9+7;
LL a[maxn],sum[maxn];
LL cnt=1; 
void init()
{
	a[1]=1;sum[1]=1;
	for(LL i=2; ;i++)
	{
		a[i]=a[i-1]*2;
		sum[i]=sum[i-1]+a[i];cnt++;
		if(sum[i]>=1e18) break;
	}
}
LL calc(LL x)
{
	LL t=1;LL cnt=0;LL ou=0;LL ji=0;LL flag=1;
	while(cnt+t<=x)
	{
		if(flag==1) ji+=t;
		else if(flag==-1) ou+=t;
		cnt+=t;
		t*=2;
		
		if(flag==1) flag=-1;
		else if(flag==-1) flag=1;
	}
	if(flag==1) ji+=x-cnt;
	else if(flag==-1) ou+=x-cnt;
	LL sum=( ((ou%mod)*(ou%mod)+ou)%mod+((ji%mod)*(ji%mod))%mod )%mod;
	return sum;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  init();
  LL l,r;cin>>l>>r;
  cout<<( ( (calc(r)-calc(l-1)))%mod+mod)%mod<<endl; 
return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值