Codeforces Round #448
Pizza Serparation
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
using std::vector;
using std::queue;
using std::map;
using std::sort;
using std::string;
#define read(x) scanf("%d",&x)
#define reads(x) scanf("%s",x) int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx > dy ? : -;
#undef datatype
} int a[], nex[];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int n;
read(n);
for (int i = ; i < n; i++) {
read(a[i]);
nex[i] = i + ;
}
nex[n - ] = ;
int ans = 0x3FFFFFFF;
for (int i = ; i < n; i++) {
int sum = ;
for (int j = i;; j = nex[j]) {
sum += a[j];
if (sum >= ) break;
}
if ( * (sum - ) < ans) ans = * (sum - );
}
printf("%d\n", ans);
return ;
}
XK Segments
STL真是手残拯救者
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<string>
using std::vector;
using std::queue;
using std::map;
using std::sort;
using std::string;
using std::lower_bound;
using std::upper_bound;
#define read(x) scanf("%d", &x)
#define reads(x) scanf("%s", x)
#define write(x) printf("%d ", x)
#define writes(x) printf("%s ", x)
#define writeln(x) printf("%d\n", x)
#define writesln(x) printf("%s\n", x)
typedef long long llint;
/*data structure*/ /*data structure*/
int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx > dy ? : -;
#undef datatype
}
/*global variable*/
map<llint, int> mp;
vector<llint> v;
int sum[];
/*global variable*/
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int n;
llint a, ap, h, l, b, x, k;
while (scanf("%d%lld%lld", &n, &x, &k) != EOF) {
mp.clear();
v.clear();
for (int i = ; i < n; i++) {
scanf("%lld", &a);
if (mp.find(a) == mp.end()) v.push_back(a);
mp[a]++;
}
sort(v.begin(), v.end());
sum[] = mp[v[]];
for (int i = ; i < v.size(); i++) sum[i] = sum[i - ] + mp[v[i]];
llint ans = ;
for (int i = ; i < v.size(); i++) {
a = v[i];
if (a % x == ) ap = a;
else ap = a - a % x + x;
if (k == ) {
h = ap - ;
l = a;
} else {
ap += x * (k - );
l = ap;
h = ap + x - ;
}
if (l > h) continue;
int s = lower_bound(v.begin(), v.end(), l) - v.begin(), e = lower_bound(v.begin(), v.end(), h) - v.begin();
if (e == v.end() - v.begin()) e--;
if (v[e] > h) e--;
if (s == v.end() - v.begin()) continue;
ans += (llint)mp[a] * (llint)(sum[e] - sum[s] + mp[v[s]]);
}
printf("%lld\n", ans);
}
return ;
}
Square Subsets
首先,其中本来就是完全平方数的数,爱取多少取多少,不影响结果,只需最终结果乘以2^a[i]
对于不是完全平方数的数,只需关心其取了奇数个还是偶数个,具体数目不影响结果,因此可以看成只取0个或1个,最终结果乘以2^(a[i]-1)。
将1-70的数分解质因数,记录每个数的各个因子有奇数个还是偶数个。
1-70间的质数共19个,用0-2^19-1表示所有状态,第i为为1表示第i个质数有奇数个,为0表示有偶数个。
dp[i]表示达到i这种质因子奇偶状态的方法数,dp[0]表示完全平方数的方法数。
最终结果减1,去掉空集。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<string>
using std::vector;
using std::queue;
using std::map;
using std::sort;
using std::string;
using std::lower_bound;
using std::upper_bound;
#define read(x) scanf("%d", &x)
#define reads(x) scanf("%s", x)
#define write(x) printf("%d ", x)
#define writes(x) printf("%s ", x)
#define writeln(x) printf("%d\n", x)
#define writesln(x) printf("%s\n", x)
#define fillchar(x, a) memset(x, a, sizeof(x))
typedef long long llint;
/*data structure*/ /*data structure*/
int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx > dy ? : -;
#undef datatype
}
/*global variable*/
const int p[] = {, , , , , , , , , , , , , , , , , , };
const llint mod = ;
int a[], v[][];
llint dp[][ << ];
/*global variable*/
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int n, x;
read(n);
fillchar(a, );
fillchar(v, );
for (int i = ; i < n; i++) {
read(x);
a[x]++;
}
int multp = ;
for (int i = ; i <= ; i++) {
multp += a[i * i];
a[i * i] = ;
}
for (int i = ; i <= ; i++) {
if (a[i]) multp += (a[i] - );
}
for (int i = ; i <= ; i++) {
x = i;
for (int j = ; j < ; j++) {
while (x % p[j] == ) {
v[i][j] = - v[i][j];
x /= p[j];
}
}
}
fillchar(dp, );
dp[][] = ;
int last = , cur;
for (int i = ; i <= ; i++) {
if (a[i] == ) continue;
cur = - last;
for (int j = ; j < ( << ); j++) dp[cur][j] = ;
for (int j = ; j < ( << ); j++) {
int state = j;
if (dp[last][state] == ) continue;
for (int k = ; k < ; k++) {
state ^= (v[i][k] << k);
}
dp[cur][state] = (dp[cur][state] + dp[last][j]) % mod;
}
for (int j = ; j < ( << ); j++) {
dp[cur][j] = (dp[cur][j] + dp[last][j]) % mod;
}
last = cur;
}
llint ans = dp[last][];
for (int i = ; i < multp; i++) {
ans = ans * % mod;
}
printf("%lld\n", ans - );
return ;
}
String Mark
设f(s)为用字符串a组合能产生比s小的字符串的个数,答案为f(b)-f(a)-1,一位一位算就可以了。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<functional>
#include<iostream>
using std::priority_queue;
using std::vector;
using std::queue;
using std::map;
using std::sort;
using std::string;
using std::lower_bound;
using std::upper_bound;
using std::cin;
using std::cout;
using std::endl;
#define fillchar(x, a) memset(x, a, sizeof(x))
typedef long long lint;
/*data structure*/ /*data structure*/
int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx > dy ? : -;
#undef datatype
}
/*global variable*/
char a[], b[];
lint fac[], ifac[];
int cnt[], n;
const lint mod = ;
/*global variable*/
/*function*/
inline lint pow(lint a, lint b, lint p) {
lint rtn = ;
while (b) {
if (b & ) rtn = rtn * a % p;
a = a * a % p;
b >>= ;
}
return rtn;
}
lint f(char * s) {
fillchar(cnt, );
for (int i = ; i < n; i++) cnt[a[i] - 'a']++;
lint div = ;
for (int i = ; i < ; i++) div = div * ifac[cnt[i]] % mod;
lint rtn = ;
for (int i = ; i < n; i++) {
for (int j = ; j < s[i] - 'a'; j++) {
if (cnt[j]) {
rtn = (rtn + fac[n - i - ] * cnt[j] % mod * div % mod) % mod;
}
}
if (cnt[s[i] - 'a'] == ) return rtn;
div = div * cnt[s[i] - 'a'] % mod;
cnt[s[i] - 'a']--;
}
return rtn;
}
/*function*/
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(), cin.tie();
cin >> a >> b;
n = strlen(a);
fac[] = ifac[] = ;
for (int i = ; i <= n; i++) {
fac[i] = fac[i - ] * i % mod;
ifac[i] = pow(fac[i], mod - , mod);
}
cout << (f(b) - f(a) - + mod) % mod << endl;
return ;
}
Eyes Closed
每个线段里随机选个数,选出来的数期望为线段里的数的平均数,设为a。对于长度为l的线段里的一个数字x,其保持不变的概率为(l-1)/l,被选出来交换的概率为1/l,则其期望为ex=x*(l-1)/l+a/l,即操作后线段里每个数字的期望为原数字乘以一个常数再加一个常数,可以用线段树来实现这个操作,把lazy标记分成两部分即可。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<string>
using std::vector;
using std::queue;
using std::map;
using std::sort;
using std::string;
using std::lower_bound;
using std::upper_bound;
#define read(x) scanf("%d", &x)
#define reads(x) scanf("%s", x)
#define write(x) printf("%d ", x)
#define writes(x) printf("%s ", x)
#define writeln(x) printf("%d\n", x)
#define writesln(x) printf("%s\n", x)
#define fillchar(x, a) memset(x, a, sizeof(x))
typedef long long llint;
/*data structure*/
template <class T> class SegmentTree {
public:
T dat, lazym, lazyp;
int leftBorder, rightBorder, mid;
SegmentTree * leftSon, * rightSon;
SegmentTree() {
leftBorder = rightBorder = -;
leftSon = rightSon = NULL;
}
void pushdown();
void pushup();
void Build(T *, int, int);
void Modify(int, int, T, T);
T Query(int, int);
void Free();
};
template<class T> void SegmentTree<T>::pushdown() {
if ((lazym != || lazyp != (T)) && leftBorder != rightBorder) {
leftSon->dat = leftSon->dat * lazym + lazyp * (leftSon->rightBorder - leftSon->leftBorder + );
rightSon->dat = rightSon->dat * lazym + lazyp * (rightSon->rightBorder - rightSon->leftBorder + );
leftSon->lazym *= lazym;
leftSon->lazyp = leftSon->lazyp * lazym + lazyp;
rightSon->lazym *= lazym;
rightSon->lazyp = rightSon->lazyp * lazym + lazyp;
}
lazym = (T);
lazyp = (T);
}
template<class T> void SegmentTree<T>::pushup() {
dat = leftSon->dat + rightSon->dat;
}
template<class T> void SegmentTree<T>::Build(T * S, int l, int r) {
if (l > r) {
return;
}
lazym = (T);
lazyp = (T);
leftBorder = l;
rightBorder = r;
mid = (leftBorder + rightBorder) >> ;
if (l == r) {
dat = S[l];
return;
}
leftSon = new SegmentTree;
leftSon->Build(S, l, mid);
rightSon = new SegmentTree;
rightSon->Build(S, mid + , r);
pushup();
}
template<class T> void SegmentTree<T>::Modify(int l, int r, T mult, T plus) {
if (l > r || l < leftBorder || rightBorder < r) {
return;
}
if (leftBorder == l && rightBorder == r) {
dat = dat * mult + plus * (rightBorder - leftBorder + );
lazym *= mult;
lazyp = lazyp * mult + plus;
return;
}
pushdown();
if (r <= mid) {
leftSon->Modify(l, r, mult, plus);
} else if (mid < l) {
rightSon->Modify(l, r, mult, plus);
} else {
leftSon->Modify(l, mid, mult, plus);
rightSon->Modify(mid + , r, mult, plus);
}
pushup();
}
template<class T> T SegmentTree<T>::Query(int l, int r) {
if (l > r || l < leftBorder || rightBorder < r) {
return dat;
}
pushdown();
if (l == leftBorder && r == rightBorder) {
return dat;
}
if (r <= mid) {
return leftSon->Query(l, r);
} else if (mid < l) {
return rightSon->Query(l, r);
} else {
return leftSon->Query(l, mid) + rightSon->Query(mid + , r);
}
}
template<class T> void SegmentTree<T>::Free() {
if (leftSon != NULL) {
leftSon->Free();
}
if (rightSon != NULL) {
rightSon->Free();
}
delete leftSon;
delete rightSon;
}
/*data structure*/
int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx > dy ? : -;
#undef datatype
}
/*global variable*/
SegmentTree<double> st;
double a[];
/*global variable*/
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int n, q;
read(n);
read(q);
for (int i = ; i < n; i++) scanf("%lf", &a[i]);
st.Build(a, , n - );
int t, l1, l2, l, r1, r2, r;
for (int i = ; i < q; i++) {
read(t);
if (t == ) {
read(l1);
read(r1);
read(l2);
read(r2);
l1--, r1--, l2--, r2--;
double sum1 = st.Query(l1, r1), sum2 = st.Query(l2, r2);
double ave1 = sum1 / (r1 - l1 + 1.0), ave2 = sum2 / (r2 - l2 + 1.0);
st.Modify(l1, r1, (r1 - l1) / (r1 - l1 + 1.0), ave2 / (r1 - l1 + 1.0));
st.Modify(l2, r2, (r2 - l2) / (r2 - l2 + 1.0), ave1 / (r2 - l2 + 1.0));
} else {
read(l);
read(r);
l--, r--;
double ans = st.Query(l, r);
printf("%.7lf\n", ans);
}
}
return ;
}
Codeforces Round #448的更多相关文章
- Codeforces Round #448 C. Square Subsets
题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...
- Codeforces Round #448(Div.2) Editorial ABC
被B的0的情况从头卡到尾.导致没看C,心情炸裂又掉分了. A. Pizza Separation time limit per test 1 second memory limit per test ...
- Codeforces Round #448 (Div. 2) B
题目描述有点小坑,ij其实是没有先后的 并且y并不一定存在于a中 判断y的个数和所给数组无关 对于2 - 7来说 中间满足%2==0的y一共有3个 2 4 6 这样 可以看出对于每个数字a 都能够二分 ...
- Codeforces Round #448 (Div. 2)C. Square Subsets
可以用状压dp,也可以用线型基,但是状压dp没看台懂... 线型基的重要性质 性质一:最高位1的位置互不相同 性质二:任意一个可以用这些向量组合出的向量x,组合方式唯一 性质三:线性基的任意一个子集异 ...
- Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】
A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- 很实用的html meta标签实现页面跳转
就算你是有很多年开发经验的web开发工程师,有着很多web开发经验,对于先进的web开发技术有着很深刻的研究,然而你却忽略了那些最最基础的东西!现在我来问你,你是否对html所有的标签都能熟练的使用呢 ...
- 从输入url到页面展示出来经历了哪些过程
本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址. 此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...
- hibernate与spring整合
Spring与Hibernate整合关键点: 1) Hibernate的SessionFactory对象交给Spring创建: 2) hibernate事务交给spring的声明式事务管理. 1. D ...
- Python 切片 day3
你可以处理列表的部分元素——Python称之为切片 . 一.使用方法: 要创建切片,可指定要使用的第一个元素和最后一个元素的索引. 与函数range() 一样,Python在到达你指定的第二个索引前面 ...
- Vue push() pop() shift() unshift() splice() sort() reverse() ...
Vue 变异方法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. shift() 方法用于把数组的第一个元素从其中删除,并返回 ...
- 洛谷P1426 小鱼会有危险吗【水题模拟】
有一次,小鱼要从A处沿直线往右边游,小鱼第一秒可以游7米,从第二秒开始每秒游的距离只有前一秒的98%.有个极其邪恶的猎人在距离A处右边s米的地方,安装了一个隐蔽的探测器,探测器左右x米之内是探测范围. ...
- Linux思维导图之计划任务
查漏补缺,理解概念,及时总结,互相交流,欢迎拍砖. 用yum install -y vixie-cron这个命令进行安装计划任务服务,可以在安装之前使用crontab -e进行检测一下,服务器是否安装 ...
- 继续聊WPF——自定义滚动条
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winf ...
- Spring Cloud-个人理解的微服务演变过程(一)
最初架构 说明:最初我们架构是垂直的 所有功能都在一个项目里面 随着业务和用户的增长 原来一台服务器已经不能支撑现有的请求数 这个时候我们就需要部署多台服务器 集群模式 说明:我们使用nginx做代理 ...
- CodeForces - 9B - Running Student
先上题目: B. Running Student time limit per test 1 second memory limit per test 64 megabytes And again ...