Problem for Nazar CodeForces - 1151C (前缀和)
Problem for Nazar
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,…1,3,5,7,…), and the second set consists of even positive numbers (2,4,6,8,…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,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 lland rr. The answer may be big, so you need to find the remainder of the division by 10000000071000000007 (109+7109+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 (1≤l≤r≤10181≤l≤r≤1018) — the range in which you need to find the sum.
Output
Print a single integer — the answer modulo 10000000071000000007 (109+7109+7).
Examples
1 3
7
5 14
105
88005553535 99999999999
761141116 题意:题目给了一个奇数集合以及一个偶数集合,第一次从奇数集合中取一个数,第二次从偶数集合中去两个数,第三次再从奇数集合中取四个数......以此类推。
给我们数字的左右边界l和r,试求区间[l,r]中元素的和是多少。
解题思路: 求区间 l 到 r 的元素等于区间[1,r]的元素和减去[1,l-1]的元素和(前缀和)
因为数字是按顺序取得, 所以只要求出区间中奇数的个数 以及 偶数的个数用等差数列求和就可以求出区间元素和。
注意:在取余1e+7之后,可能区间[1.r]的元素和小于[1,l-1]的元素和 所以两者做差之后还要再加上1e9+7 在进行取余!!!
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<set>
#include<stack>
#include<queue>
using namespace std;
const int maxn = ;
const int mod = 1e9 + ;
typedef long long ll;
ll l,r;
ll getsum(ll x){
ll num1 = ,num2 = ;
ll l1 = x;
ll mm = ;
ll nn = ;
while(l1 > ){
if(l1 >= mm){
if(nn % == ){
num1 += mm;
}else{
num2 += mm;
}
l1 -=mm;
}else{
if(nn % == ){
num1 += l1;
}else{
num2 += l1;
}
l1 = ;
}
nn++;
mm *=;
}
ll sum = ;
sum = sum +(((num2 % mod)*((num2 + )%mod))%mod +((num1%mod)*(num1%mod))%mod)%mod;
return sum%mod;
}
int main(){
scanf("%lld %lld",&l,&r);
ll a = getsum(r);
ll b = getsum(l - );
printf("%lld\n",(getsum(r) - getsum(l - ) + mod)%mod);
return ;
}
AC代码
一个从很久以前就开始做的梦。
最新文章
- http协议get、post请求分析及用HttpRequester测试的报错及可能原因
- Sound Generator 原理
- Android Studio在导入eclipse的项目时一直卡在gradle:Configure project
- react入门笔记
- 10款Windows命令行工具
- IntelliJ IDEA 15激活
- Java应用短信猫
- MonkeyRunner 连续两次点击报“Error sending touch event”
- Delphi Socket的最好项目——FastMsg IM(还有一些IM控件),RTC,RO,Sparkle等等,FileZilla Client/Server,wireshark,NSClient
- C# winform Datagridview 标题居中
- BFS-九宫格重排(详解)
- Android 6.0 默认关闭定位和GPS,开启后默认选省电
- luogu P4515 [COCI2009-2010#6] XOR
- mysql之mysqldump——备份与还原
- nodejs开篇基础<;①>;
- Java Netty (2)
- LHC大神问的矩阵转置问题
- Python的isdigit()和isalpha()
- Java注解Annotation(一)
- Connecting to a Remote Serial Port over TCP/IP