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$个矩阵是由初始矩阵区间赋值得到的, 求选择一个新矩阵, 使得其余新矩阵到它距离和最小. 字符集比较小, 可以考虑每次区间覆盖对每个字符的贡献. ...
随机推荐
- Flink Task 并行度
并行的数据流 Flink程序由多个任务(转换/运算符,数据源和接收器)组成,Flink中的程序本质上是并行和分布式的. 在执行期间,流具有一个或多个流分区,并且每个operator具有一个或多个ope ...
- 通过request获得全路径
<% String test = request.getScheme()+"://"+request.getServerName()+":"+reque ...
- Java的Regex --正则表达式
一.概述 正则表达式通常被用来对字符串提供范围性的校验.替换那些符合某个模式(规则)的文本. 正则表达式所对应的类Pattern,所有的正则表达式都是在这个类下创建的.Pattern类用于创建一个正则 ...
- java基础源码 (2)--StringBuilder类
Serializable(接口): 是一个IO的序列化接口,实现了这个接口,就代表这个类可以序列化或者反序列化,该接口没有方法或者字段,仅用于标识可串行话的语义. Appendable(接口): /* ...
- 51nod 1103:N的倍数 抽屉原理
1103 N的倍数 题目来源: Ural 1302 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为N的数组A,从A中选出若干个数,使得这 ...
- LARGE_INTEGER 64位的输出格式
%016I64x 第一个016是指当最左边无数据时用00填充:后面的I64x是__int64的前缀要求格式十六进制输出.
- Webpack - 把json文件打包进js文件
把html文件打包进index.js 1 新建文件 typings.d.ts declare module "*.html" { const content: st ...
- cf 187B.AlgoRace
floyd...太神奇了(不会floyd(大雾)) 貌似floyd的外层k是保证最短路从起点逐渐向外扩展(而不是乱搞233) 所以在处理f[i][j]=min(f[i][j],f[i][k]+f[k] ...
- CMD命令实现数字雨
首先,我们在桌面上先创建一个“.bat”格式的文件,文件名为“数字雨.bat”,创建好了之后右键点击编辑. 在其中输入: @echo off title 数字雨 color 0a setlocal E ...
- 一行python代码能写出啥?
1.一行代码启动一个Web服务 python -m SimpleHTTPServer 8080 # python2 python3 -m http.server 8080 # python3 2. ...