Codeforces Round #553 (Div. 2) C题

题目网址:http://codeforces.com/contest/1151/problem/C

题目大意:给定奇数集和偶数集,现构造一个数组,先取奇数集中一个元素1,再取偶数集二个元素2,4,再取奇数集四个元素3,5,7,9,再取偶数集八个元素,6,8,10……

得到 1,2,4,3,5,7,9,6,8,10,12……问这个数组的某一区间和是多少,并对1e9+7取模。

题解:对于奇数集x和偶数集y的前k项,有x=k^2,y=k*(k+1),首先,计算区间和,可以用前缀和,当计算前n项的和时,只需判断前n项有多少个奇数和偶数即可,要注意取模作差的时候可能出现负数的情况。

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int mod=1e9+7;
 5 ll cal(ll x)
 6 {
 7     ll odd=0,even=0,flag=1,c=1;
 8     while(x) {
 9         if(flag==1) {
10             odd+=min(c,x);x-=min(x,c);
11         }
12         else {
13             even+=min(c,x);x-=min(x,c);
14         }
15         flag=-flag;c<<=1;
16     }
17     ll ans=(odd%mod)*(odd%mod)%mod+(even%mod)*((even+1)%mod)%mod;
18     return ans%mod;
19 }
20 int main()
21 {
22     ll l,r;
23     cin>>l>>r;
24     cout<<(cal(r)-cal(l-1)+mod)%mod<<endl;
25 }  

原文地址:https://www.cnblogs.com/duxing201806/p/10778304.html

时间: 2024-08-02 09:53:21

Codeforces Round #553 (Div. 2) C题的相关文章

Codeforces Round #553 (Div. 2) B题

题目网址:http://codeforces.com/contest/1151/problem/B 题目大意:给定一个n*m的矩阵,问是否可以从每一行中选择一个数,使得这n个数异或大于0,如果可以还要输出它们的列位置 题解:首先如果a^b==0,b!=c,则a^c>0.那么考虑构造,为了方便,选取第一列的数,如果异或>0,直接输出列位置,反之则随便在一行中,找到一个与第一个不相等的数,那么由异或性质满足条件,如果n行都找不到,则无法实现. 1 #include<bits/stdc++.h

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Codeforces Round #243 (Div. 1) A题

http://codeforces.com/contest/425/problem/A 题目链接: 然后拿出这道题目是很多人不会分析题目,被题目吓坏了,其中包括我自己,想出复杂度,一下就出了啊!真是弱! 直接暴力求出矩阵数值,然后枚举每一个[I,J];再O[N]判断,分配好在[I,J]区间的数和之内的数,再排序下SOLO了 CODE:#include <cstdio> #include <cstring>#include <queue>#include <vect

Codeforces Round #634 (Div. 3) 补题

A. Candies and Two Sisters 签到题,直接输出即可 代码 #include<bits/stdc++.h> #define INF 0x3f3f3f3f typedef long long ll; using namespace std; inline void read(int &p) { p=0;int flag=1;char c=getchar(); while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();} w

Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discov

Codeforces Round #374 (div.2)遗憾题合集

C.Journey 读错题目了...不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图) 并且这题因为要求路径点尽可能多 其实可以规约为限定路径长的拓扑排序,不一定要用最短路做 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm>

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

Codeforces Round #419 (Div. 1) 补题 CF 815 A-E

A-C传送门 D Karen and Cards 技巧性很强的一道二分优化题 题意很简单 给定n个三元组,和三个维度的上限,问存在多少三元组,使得对于给定的n个三元组中的每一个,必有两个维度严格小于. 首先我们根据一个维度(c维)对n个三元组排序,然后枚举答案在这个维度的取值. 此时序列被分成了两个部分,前半部分 满足所有c大于等于i 后半部分满足所有c严格小于i(即已有一个维度小于) 通过累计,我们知道此时前半部a维的最大值ma和b维的最大值mb. 显然可能存在的三元组答案,必然首先满足a维和

Codeforces Round #367 (Div. 2) 套题

吐槽:只能说是上分好场,可惜没打,唉 A:Beru-taxi (水题,取最小值) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string>