NOIP模拟题 2017.11.6

题目大意 给定一个大小为n的数组,从中选出一个子集使得这个子集中的数的和能被n整除。
假设开始我没有做出来,那么我就random_shuffle一下,然后计算前缀和,有一个能被n整除,就输出答案。于是这道题就过了。(数据水得起飞)
考虑计算前缀和,如果存在两个前缀和在模n的意义同余,那么就有可以将两个前缀和相减得到的一段区间的和,它的和就是n的倍数。
考虑这么做的正确性,模n的意义下有n个数,但是前缀和总共有(n + 1)个数。
Code
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <set>
#include <map>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
typedef pair<int, int> pii;
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
template<typename T>
inline void readInteger(T& u) {
char x;
while(!isdigit(x = getchar()));
+ x - ');
}
int n;
int* a;
;
int L, R;
inline void init() {
readInteger(n);
a = )];
memset(a, -, ));
a[] = ;
, x; i <= n; i++) {
readInteger(x);
s = (s + x) % n;
) {
L = a[s] + , R = i;
return;
}
a[s] = i;
}
}
inline void solve() {
printf();
for(int i = L; i <= R; i++)
printf("%d ", i);
}
int main() {
freopen("set.in", "r", stdin);
freopen("set.out", "w", stdout);
init();
solve();
;
}


题目大意 有n本书,每次只能选择和上次种类不同的书阅读,问最少有多少本书看不了。
仔细分析题目可以得到一个信息:如果种类最多的那本书的数量大于n的一半,那么答案就是它的两倍减n减1。否则答案为0。
由于我只关心出现次数超过一半的众数的出现次数,因此有了以下三种解法(2骗分 + 1正解)
Solution 1 (抽样法I)
随机抽取一些位置,求出它们的众数,然后再带进原序列中求出现次数。
Code
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <set>
#include <map>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
typedef pair<int, int> pii;
#define ll long long
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
template<typename T>
inline void readInteger(T& u) {
char x;
while(!isdigit(x = getchar()));
+ x - ');
}
int m, k;
;
int *counter;
int *X, *Y, *Z, S;
int myrand() {
| rand();
}
inline void init() {
readInteger(m);
readInteger(k);
counter = )];
X = )];
Y = )];
Z = )];
S = ( << k) - ;
; i <= m; i++) {
readInteger(counter[i]);
n += counter[i];
}
; i <= m; i++)
readInteger(X[i]);
; i <= m; i++)
readInteger(Y[i]);
; i <= m; i++)
readInteger(Z[i]);
}
;
int pos[randTime];
inline void solve() {
; i < randTime; i++)
pos[i] = myrand() % n;
sort(pos + , pos + randTime);
, pos + randTime) - pos;
, cnt = , id = -;
; i <= m && p < len; i++) {
last = X[i];
if(cnt == pos[p])
pos[p++] = last;
cnt++;
; j < counter[i]; j++, cnt++) {
last = (last * 1LL * Y[i] + Z[i]) & S;
if(cnt == pos[p])
pos[p++] = last;
}
}
pos[] = -;
sort(pos + , pos + len);
, maxcnt = ;
; i < len; i++) {
])
cmp = ;
if(++cmp > maxcnt)
maxcnt = cmp, id = pos[i];
}
cnt = ;
; i <= m; i++) {
cnt += (last = X[i]) == id;
; j < counter[i]; j++)
cnt += (last = (last * 1LL * Y[i] + Z[i]) & S) == id;
}
)
printf(");
else
printf( * cnt - n - );
}
int main() {
freopen("read.in", "r", stdin);
freopen("read.out", "w", stdout);
srand();
init();
solve();
;
}
read (Random I)
Solution 2 (抽样法II)
抽取每一段前10个,求出它们的众数,然后再带回原序列中求出现次数。
显然数据很水所以过了,然后求众数时,我没有排序,还是过了(这。。。)
Code
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <set>
#include <map>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
typedef pair<int, int> pii;
#define ll long long
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
template<typename T>
inline void readInteger(T& u) {
char x;
while(!isdigit(x = getchar()));
+ x - ');
}
int m, k;
;
];
], Y[], Z[], S;
int myrand() {
| rand();
}
inline void init() {
readInteger(m);
readInteger(k);
S = ( << k) - ;
; i <= m; i++) {
readInteger(counter[i]);
n += counter[i];
}
; i <= m; i++)
readInteger(X[i]);
; i <= m; i++)
readInteger(Y[i]);
; i <= m; i++)
readInteger(Z[i]);
}
];
inline void solve() {
;
; i <= m; i++) {
pos[++len] = X[i];
; j <= counter[i] && j < ; j++)
pos[++len] = (pos[len - ] * Y[i] + Z[i]) & S;
}
pos[] = -;
, id, maxcnt = , cnt, last;
; i < len; i++) {
])
cmp = ;
if(++cmp > maxcnt)
maxcnt = cmp, id = pos[i];
}
cnt = ;
; i <= m; i++) {
cnt += (last = X[i]) == id;
; j < counter[i]; j++)
cnt += (last = (last * Y[i] + Z[i]) & S) == id;
}
)
printf(");
else
printf( * cnt - n - );
}
int main() {
freopen("read.in", "r", stdin);
freopen("read.out", "w", stdout);
// srand(233);
init();
solve();
;
}
read (Random II)
Solution 3 (求和法)
因为它出现次数大于一半,所以考虑用一个 cnt 和一个 id
枚举序列中每个数,如果 cnt == ,那么就将 id 赋值为当前枚举的这个数,并将cnt置为1。
否则,如果当前的这个数和 id 相等,就将 cnt 的值加1,否则减1。
这个算法完成后,我们会得到一个是出现次数超过n的一半的众数或者一个诡异的数,最后再把得到的id带回去求次数。
这么做的正确性显然(虽然解释不了但是觉得显然正确啊)。
Code
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <set>
#include <map>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
typedef pair<int, int> pii;
#define ll long long
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
template<typename T>
inline void readInteger(T& u) {
char x;
while(!isdigit(x = getchar()));
+ x - ');
}
int m, k;
;
int *counter;
int *X, *Y, *Z, S;
inline void init() {
readInteger(m);
readInteger(k);
counter = )];
X = )];
Y = )];
Z = )];
S = ( << k) - ;
; i <= m; i++) {
readInteger(counter[i]);
n += counter[i];
}
; i <= m; i++)
readInteger(X[i]);
; i <= m; i++)
readInteger(Y[i]);
; i <= m; i++)
readInteger(Z[i]);
}
, id;
inline void add(int x) {
)
id = x, cnt = ;
else if(id == x)
cnt++;
else
cnt--;
}
inline void solve() {
int last;
; i <= m; i++) {
add(last = X[i]);
; j < counter[i]; j++)
add(last = (last * 1LL * Y[i] + Z[i]) & S);
}
cnt = ;
; i <= m; i++) {
cnt += (last = X[i]) == id;
; j < counter[i]; j++)
cnt += (last = (last * 1LL * Y[i] + Z[i]) & S) == id;
}
)
printf(");
else
printf( * cnt - n - );
}
int main() {
freopen("read.in", "r", stdin);
freopen("read.out", "w", stdout);
init();
solve();
;
}


题目大意 (题目太简洁,无法概括大意)
因为涉及到了可恶的位运算,为了更好地处理它们,所以想到Trie树。
如果Trie树的一个非叶节点在两天中表示的名次在a ~ b之间,设它的两棵子树的大小分别为s1和s2。
那么左子树表示的区间就是a ~ (a + s1 - 1)和(a + s2) ~ b,右子树同理。
因为最终到了叶节点,表示的区间都变成a ~ a的形式,并且我们关心的只是平方和。
所以考虑如何维护所有开始端点的平方和。
写写式子发现:

由于然后发现再维护一下所有左端点的和就可以搞定了。
写代码的时候可以用黑科技优化,不建Trie树就可以直接搞答案。先将A数组排序,然后对于每一层都进行二分查找这一位0和1的分界位置。
Code
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <set>
#include <map>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
typedef pair<int, int> pii;
#define ll long long
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
template<typename T>
inline void readInteger(T& u) {
static char x;
while(!isdigit(x = getchar()));
+ x - ');
}
;
int n, m;
;
ll S;
int* A;
inline void init() {
readInteger(n);
readInteger(m);
A = )];
S = (1ll << (m - ));
; i <= n; i++)
readInteger(A[i]);
}
void dfs(int dep, int L, int R, ll sum, ll sum2) {
if(L == R) {
ans ^= (sum2 % M);
return;
}
int l = L, r = R;
while(l <= r) {
;
<< dep)) r = mid - ;
;
}
, s2 = R - r;
, L, r, sum + S * s2, sum2 + (ll)sum * s2 + S * s2 * s2);
, r + , R, sum + S * s1, sum2 + (ll)sum * s1 + S * s1 * s1);
}
inline void solve() {
sort(A + , A + n + );
dfs(m - , , n, , );
printf("%d", ans);
}
int main() {
freopen("race.in", "r", stdin);
freopen("race.out", "w", stdout);
init();
solve();
;
}
NOIP模拟题 2017.11.6的更多相关文章
- NOIP模拟题 2017.7.3 - 模拟 - 贪心 - 记忆化搜索
直接暴力模拟,注意判数据结构为空时的取出操作. Code #include<iostream> #include<cstdio> #include<ctime> # ...
- noip模拟题 2017.10.28 -kmp -Tarjan -鬼畜的优化
题目大意 给定A串,选择A串的前lB个字符作为B串,再在B串后增加一个字符,问最长的相等的A串前缀和B串的后缀. Solution 1(KMP) 用1个奇怪的字符连接A串和B串,再用KMP求最长公共前 ...
- NOIP模拟题汇总(加厚版)
\(NOIP\)模拟题汇总(加厚版) T1 string 描述 有一个仅由 '0' 和 '1' 组成的字符串 \(A\),可以对其执行下列两个操作: 删除 \(A\)中的第一个字符: 若 \(A\)中 ...
- 【入门OJ】2003: [Noip模拟题]寻找羔羊
这里可以复制样例: 样例输入: agnusbgnus 样例输出: 6 这里是链接:[入门OJ]2003: [Noip模拟题]寻找羔羊 这里是题解: 题目是求子串个数,且要求简单去重. 对于一个例子(a ...
- 9.9 NOIP模拟题
9.9 NOIP模拟题 T1 两个圆的面积求并 /* 计算圆的面积并 多个圆要用辛普森积分解决 这里只有两个,模拟计算就好 两圆相交时,面积并等于中间两个扇形面积减去两个三角形面积 余弦定理求角度,算 ...
- 8.22 NOIP 模拟题
8.22 NOIP 模拟题 编译命令 g++ -o * *.cpp gcc -o * *.c fpc *.pas 编译器版本 g++/gcc fpc 评测环境 位 Linux, .3GHZ CPU ...
- NOIP模拟赛-2018.11.7
NOIP模拟赛 如果用命令行编译程序可以发现没加头文件之类的错误. 如果用命令行编译程序可以发现没加头文件之类的错误. 如果用命令行编译程序可以发现没加头文件之类的错误. 编译之前另存一份,听说如果敲 ...
- NOIP模拟赛-2018.11.6
NOIP模拟赛 今天想着反正高一高二都要考试,那么干脆跟着高二考吧,因为高二的比赛更有技术含量(我自己带的键盘放在这里). 今天考了一套英文题?发现阅读理解还是有一些困难的. T1:有$n$个点,$m ...
- NOIP模拟题17.9.26
B 君的任务(task)[题目描述]与君初相识,犹如故人归.B 君看到了Z 君的第一题,觉得很难.于是自己出了一个简单题.你需要完成n 个任务,第i 任务有2 个属性ai; bi.其中ai 是完成这个 ...
随机推荐
- DX9 空间坐标变换示例代码
// @time 2012.3.25 // @author jadeshu #include <Windows.h> #include <d3d9.h> #include &l ...
- 11.match
(我对部分段落进行翻译) A match statement is used to branch execution of a program. It’s the equivalent of the ...
- Vue系列之 => 动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- django之视图view小知识
CBV简版流程 AddPublisher.as_view() ——> view 函数 当请求来的时候才执行view view中执行: 1. 先实例化AddPublisher,给self def ...
- MQ的使用场景
一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,RabbitM ...
- 大数据权限管理工具 Apache Ranger 初识
资料参考: Apache Ranger – Introduction http://ranger.apache.org/ 阿里云 Ranger简介 Apache Ranger初识 - 阿里云 大数据权 ...
- centos 安装arcgis server 10.1
1.创建新用户,不要在root下面直接安装 [root@localhost ~]# groupadd esri //创建esri组 [root@localhost ~]# useradd ags -g ...
- 今天2.4寸tft触摸屏到手--刷屏驱动小结
2010-04-29 21:28:00 根据给的51程序改成了iccavr,结果改错了2处.导致我找原因找了n久.不过也是一件好事,让我对80i更加熟悉了. 通过protues的逻辑分析仪,找到了问题 ...
- JustOj 1414: 潘神的排序
题目描述 老潘,袁少,小艾都是江理的大个子,他们想按身高排队,现在给你他们的身高,请你算出队伍中站在第二的有多高. 输入 输入三个整数,分别表示三个人的身高.(单位:纳米) 输出 输出身高排第二的身高 ...
- hud1007 Quoit Design
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> # ...