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$个矩阵是由初始矩阵区间赋值得到的, 求选择一个新矩阵, 使得其余新矩阵到它距离和最小. 字符集比较小, 可以考虑每次区间覆盖对每个字符的贡献. ...
随机推荐
- java的JDBC的事务学习
https://www.cnblogs.com/chy18883701161/p/11372089.html
- Win7微信DLL劫持反弹SHELL(10.9 第十七天)
(该文参考自网络其他人资料,仅为学习,不得用于非法用途) 准备的工具:kali虚拟机 W7虚拟机 微信 ProcessExplorer the-backdoor-factory-master 打开微信 ...
- laravel自动加载公共文件
1. 创建 functions.php 在 app/Common/(目录自己起名字)下新建一个文件 functions.php,在内部补充如下代码: <?php /** 数据返回 * 返回jso ...
- kettle将csv文件导入数据库
具体过程学习了: 1.连接数据库 2.添加新资源库 3.选择Other Repositories 4.选择Database Repository,第二个需要配置额外参数 5.连接数据库相关设置 6.连 ...
- Kmeans应用
1.思路 应用Kmeans聚类时,需要首先确定k值,如果k是未知的,需要先确定簇的数量.其方法可以使用拐点法.轮廓系数法(k>=2).间隔统计量法.若k是已知的,可以直接调用sklearn子模块 ...
- Vulkan SDK 之 Device
Enumerate Physical Devices Vulkan instance创建完成之后,vulkan loader是知道你有几个物理设备(显卡),但是程序不知道,需要通过 相关接口获取设备 ...
- cnblogs今天挂了
刚打算搬进来常驻cnblogs,好死不死,它今天11点40分左右挂了.真是不让人省心: 今天把这后面这几个月的随笔放过来,希望能不会被清掉:
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring体系结构详解
Spring 框架采用分层架构,根据不同的功能被划分成了多个模块,这些模块大体可分为 Data Access/Integration.Web.AOP.Aspects.Messaging.Instrum ...
- ArrayList源码阅读笔记
ArrayList ArrayList继承自AbstractList抽象类,实现了RandomAccess, Cloneable, java.io.Serializable接口,其中RandomAcc ...
- 判断苹果和安卓端或者wp端
window.onload = function() { var u = navigator.userAgent; if(u.indexOf('Android') > -1 || u.index ...