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代码
一个从很久以前就开始做的梦。
Problem for Nazar CodeForces - 1151C (前缀和)的更多相关文章
- CodeForces 1151C Problem for Nazar
题目链接:http://codeforces.com/problemset/problem/1151/C 题目大意: 有一个只存奇数的集合A = {1, 3, 5……2*n - 1,……},和只存偶数 ...
- Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinat ...
- Codeforces Round #553 (Div. 2) C. Problem for Nazar 数学
题意:从奇数列 1 3 5 7 9 .... 偶数列2 4 6 8 10...分别轮流取 1 2 4 ....2^n 个数构成新数列 求新数列的区间和 (就一次询问) 思路:首先单次区间和就是一个简 ...
- Codeforces Round #553 (Div. 2) 【C. Problem for Nazar】
题目大意: 一开始第一行是 1,第二行是2 4 ,第三行是3 5 7 9 ,类似这样下去,每一行的个数是上一行的个数,然后对这些点从第一个进行编号,问你从[l,r]区间数的和. 思路:分别求出奇数和偶 ...
- Codeforces 612D 前缀和处理区间问题
传送门:http://codeforces.com/problemset/problem/612/D (转载请注明出处谢谢) 题意: 给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri ...
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- A Boring Problem UVALive - 7676 (二项式定理+前缀和)
题目链接: I - A Boring Problem UVALive - 7676 题目大意:就是求给定的式子. 学习的网址:https://blog.csdn.net/weixin_37517391 ...
- CodeForces 816B 前缀和
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Dasha and Photos CodeForces - 761F (前缀优化)
大意: 给定n*m初始字符矩阵, 有k个新矩阵, 第$i$个矩阵是由初始矩阵区间赋值得到的, 求选择一个新矩阵, 使得其余新矩阵到它距离和最小. 字符集比较小, 可以考虑每次区间覆盖对每个字符的贡献. ...
随机推荐
- 六十九、SAP中内表插入的三种方法之三,INSERT的使用,用于指定位置插入
一.代码如下 二.需要注意的时候,如果内表和工作区同名,这可以用隐式插入,不需要什么工作区INTO到什么表,INDEX为位置,效果图如下:
- 101-PHP二维数组的元素输出三,封装成函数
<?php $arr=array(array(76,87,68), array(65,89,95), array(90,80,66), array(90,95,65),5,234,56,'Hel ...
- C# 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。
今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系". ...
- springboot项目 线程消费队列注入报错误空指针
背景: 在调用阿里云隐私保护有一个通话记录的回执消息是一个消费线程队列,这个还别人告诉我的,因为我根本没有看出来哪里是个线程了,然后我就把它当成普通的代码拿到返回值以后然后插入数据库 可是我这边该加的 ...
- cf 1241 D. Sequence Sorting(思维)
题意: 一个序列有n个数,有一种操作,你可以选一个数x,使这个序列中等于x的数都移到序列头或尾. 问最少几次操作后,可以使这个序列非降序. 思路: (以下说bi移动到哪里,其实就是指a1……an中等于 ...
- Canvas基本定义
Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0.今天我们主要要了解的是2D相关的 大部分2D使用的api都在android.g ...
- 【新年呈献】高性能网络通信框架 HP-Socket v5.7.1
项目主页 : http://www.oschina.net/p/hp-socket 开发文档 : https://www.docin.com/p-2287339564.html 下载地址 : http ...
- HandyJSON.Metadata.Class Xcode10.2, swift5.0 报错 linker command failed with exit code 1
https://blog.csdn.net/weiwandaixu_/article/details/88842491 2019年03月27日 13:35:40 一如初夏丿 阅读数:31 标签: li ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- c# 循环界面控件
在 Winform 开发中,窗体(Form)就像一个大容器,可以装各种各样的控件,包括 Panel控件.如果窗体是 Winform 中的最大的容器,那么 Panel控件可以算是老二,它专门用于软件界面 ...