NOIP模拟·20141105题解
【A、韩信点兵】
结论题+模板题,用到了中国剩余定理,维基百科上讲的就比较详细,这里就不再赘述了……
对于这题,我们先利用中国剩余定理($x \equiv \sum{(a_i m_i (m_i^{-1} \mod p_i))}\, \mod (\prod{p_i})$)找到当前人数的最小可行解$x_0$,(如果$x_0$已经超过了$N$,直接输出无解即可)这时不难证明,对于任何一个可行解,都有 $$x_i = x_0 + k \times \prod{P_i},k \in \mathbb{N}$$
我们的目标是找到不超过$N$的最大可行解x',那么答案就是$N - x'$。注意这里如果直接枚举$k$的话有一个测试点会超时(看起来我的数据规模还是挺良心的……只卡掉了10分)。正确的做法是先用$N$减去$x_0$,这时剩下的部分就是$N - x' + k \times \prod{P_i},k \in \mathbb{N}$,那么我们只需用这个结果对$\prod{P_i}$取余即可得到答案。
2 #include <cctype>
3 #include <algorithm>
4 #include <cstring>
5 #include <queue>
6 #include <cmath>
7 #include <iostream>
8
9
typedef long long LL;
using namespace std;
inline int lowbit(int x){return x & -x;}
int gcd(int a, int b){return (!a) ? b : gcd(b % a, b);}
template<typename T>T exgcd(T a, T b, T &x, T &y){
, y = ; return b;}
T d = exgcd(b % a, a, y, x);
x -= (b/a) * y;
return d;
}
/*=====================================*/
inline LL inv(LL a, LL mod){
LL x, y;
);
return (x % mod + mod) % mod;
}
LL n, M = , N[], e, S = ;
], a[], m;
inline void init(){
cin >> n >> m;
;i < m;++i){
cin >> P[i] >> a[i];
M *= P[i];
}
;i < m;++i){
N[i] = M / P[i];
e = N[i] * inv(N[i], P[i]) % M;
S = (S + e * a[i]) % M;
}
}
inline void work(){
LL x, y;
exgcd<LL>(, M, x, y);
x = ((x * S % M) + M ) % M;
<< endl;return;}
cout << (n - x) % M << endl;
}
int main(){
#if defined DEBUG
freopen("test", "r", stdin);
#else
freopen("HanXin.in", "r", stdin);
freopen("HanXin.out", "w", stdout);
#endif
init();
work();
;
}
中国剩余定理
【B、月考统计】
经典模型——差分约束系统。设第i位同学的分数为$x_i$,所有同学的最低分数为0. 则统计表中的每条息$i,j,a_{ij}$都可以形式化为 $$x_i - x_j \le a_{ij}$$。
对于这样一组不等式,我们可以抽象化出图论模型:每个同学都抽象为一个节点,再设一个起点0,表示所有同学的最低分数。对于每个不等式$x_i - x_j \le a_{ij}$,我们都从点i到点j连一条权值为$-a_{ij}$的有向边,表示从起点到点j的最长路权和最少比起点到i的最长路权和大$-a_{ij}$(可以用最长路的三角形不等式证明)。对于这样的图,用spfa求出从起点到每个点的最长路权和就是答案。
2 /*======================================================Code by Asm.Def========================================================*/
3 /*=============================================================================================================================*/
4 #include <cstdio>
5 #include <iostream>
6 #include <algorithm>
7 #include <cmath>
8 #include <cctype>
9 #include <memory.h>
#include <vector>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <ctime>
#include <iterator>
#include <functional>
#include <cstdlib>
using namespace std;
/*===================================================================================*/
#define forall(it,v) for(__typeof(v.begin()) it = v.begin();it < v.end();++it)
#define pb push_back
#define REP(i,j,k) for(i = j;i <= k;++i)
#define REPD(i,j,k) for(i = j;i >= k;--i)
#define iter(v) v::iterator
typedef long long LL;
template<typename T> void getint(T &x){
char c = getchar();
while(!isdigit(c))c = getchar();
x = c - ';
+ c - ';
}
/*============================================================================================*/
+ ;
int N, M;
struct edge{
int to, w;
edge(int T, int W):to(T), w(W){}
};
vector<edge> adj[maxn];
queue<int> Q;
};
}, cnt[maxn] = {};
inline void init(){
cin >> N >> M;
int i, x, y, a;
while(M--){
cin >> x >> y >> a;
adj[x].push_back(edge(y, -a));
}
;i <= N;++i)
Q.push(i);inQ[i] = ;cnt[i] = ;
}
inline void work(){
int i, t;
iter(vector<edge>) it;
while(!Q.empty()){
t = Q.front();Q.pop();inQ[t] = ;
for(it = adj[t].begin();it != adj[t].end();++it)
if(dis[t] + it->w > dis[it->to]){
dis[it->to] = dis[t] + it->w;
if(!inQ[it->to]){
if(cnt[it->to] == N){
cout << "SOMEONE LAY!" << endl;
return;
}
Q.push(it->to);inQ[it->to] = ;++cnt[it->to];
}
}
}
;i <= N;++i)
cout << dis[i] << ' ';
}
int main(){
#ifdef DEBUG
freopen("test", "r", stdin);
#else
freopen("ExamStat.in", "r", stdin);
freopen("ExamStat.out", "w", stdout);
#endif
init();
work();
#ifdef DEBUG
//cout << endl << (double)clock() / CLOCKS_PER_SEC <<endl;
#endif
;
}
spfa解差分约束
【C、神奇的压缩机】
神奇的压缩机,神奇的阅读题……
这题改编自第21场Andrew Stankevich's Contest(俄国的ACM多校训练赛)的Lempel-Ziv Compression……
当时我的解法是预处理出字符串中每个子串的“满足i小于子串长度且i前缀与i后缀相等的i”的最大值(或 原串的每个后缀的KMP-next数组)……听起来相当拗口,不过套用KMP的预处理过程可以降低思维难度。
2 // Submission Date: 2014-10-02 16:34:32
3 // Time: 8256MS
4 // Memory: 34624KB
5
6 /*======================================================Code by Asm.Def========================================================*/
7 #include <cstdio>
8 #include <iostream>
9 #include <algorithm>
#include <cmath>
#include <cctype>
#include <memory.h>
#include <cstring>
#include <cstdlib>
using namespace std;
#define maxn ((int)4.1e3)
typedef long long LL;
char ch[maxn];
, minbit[maxn], *next[maxn];
int l[maxn], r[maxn], str[maxn];
inline void getnext(int l){
int i, j, L = len - l;
next[l] = ];
int *Next = next[l];
Next[] = ;
;i < L;++i){
j = Next[i-] - ;
] && j >= )
j = Next[j] - ;
])
Next[i] = j + ;
;
}
}
void printpro(int i){
if(str[i] == i){
);
int j;
for(j = r[i];j <= i;++j)putchar(ch[j]);
return;
}
printpro(str[i]);
printf("(%d,%d)", r[i], l[i]);
}
int main(){
#ifdef DEBUG
assert(freopen("test","r",stdin));
#endif
char c;
while(isalpha(c = getchar()))str[len] = len, ch[len++] = c;
int i, j, Min, t;
;i < len - ; ++i)
getnext(i);
minbit[] = ;
;i < len; ++i){
Min = 0x7fffffff;
;j < i;++j)
< Min){
Min = minbit[j] + (i-j)*;
str[i] = i;
r[i] = j+;
}
;j < i; ++j){
t = next[j][i-j];
if(!t)continue;
< Min){
Min = minbit[i-t] + ;
str[i] = i-t;
r[i] = i+-t-j;
l[i] = t;
}
}
minbit[i] = Min;
}
printf(]);
printpro(len-);
;
}
动态规划
NOIP模拟·20141105题解的更多相关文章
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 「题解」NOIP模拟测试题解乱写II(36)
毕竟考得太频繁了于是不可能每次考试都写题解.(我解释个什么劲啊又没有人看) 甚至有的题目都没有改掉.跑过来写题解一方面是总结,另一方面也是放松了. NOIP模拟测试36 T1字符 这题我完全懵逼了.就 ...
- HZOJ 20190818 NOIP模拟24题解
T1 字符串: 裸的卡特兰数题,考拉学长讲过的原题,就是bzoj3907网格那题,而且这题更简单,连高精都不用 结论$C_{n+m}^{n}-C_{n+m}^{n+1}$ 考场上10min切掉 #in ...
- 「题解」NOIP模拟测试题解乱写I(29-31)
NOIP模拟29(B) T1爬山 简单题,赛时找到了$O(1)$查询的规律于是切了. 从倍增LCA那里借鉴了一点东西:先将a.b抬到同一高度,然后再一起往上爬.所用的步数$×2$就是了. 抬升到同一高 ...
- HGOI NOIP模拟4 题解
NOIP国庆模拟赛Day5 题解 T1 马里奥 题目描述 马里奥将要参加 NOIP 了,他现在在一片大陆上,这个大陆上有着许多浮空岛,并且其中一座浮空岛上有一个传送门,马里奥想要到达传送门从而前往 N ...
- [NOIP模拟13]题解
A.矩阵游戏 其实挺水的? 考场上根本没有管出题人的疯狂暗示(诶这出题人有毛病吧这么简单的东西写一大堆柿子),而且推公式能力近乎没有,所以死掉了. 很显然乘法有交换率结合率所以操作顺序对最终结果没什么 ...
- 8.3 NOIP 模拟12题解
话说这次考试T1和T2是真的水,然而T1CE,T2TLE,T3CE 这不就是在侮辱我的智商啊!之前本机编译都是c++,以后要用c++11. 这次的T1就是一个大型找规律,我的规律都找出来了,但是竟然用 ...
- HZOJ 20190819 NOIP模拟26题解
考试过程: 照例开题,然后觉得三道题都挺难,比昨天难多了(flag×1),T1 dp?T2 数据结构? T3 dp?事实证明我是sb然后决定先搞T2,但是,woc,这题在说什么啊,我怎么看不懂题啊,连 ...
- [NOIP模拟26]题解
今天的考试题改自闭了……所以滚来写陈年题解. A.*****贪婪***** RT,出题人告诉我们这题要贪心. 最优的策略一定是拖到必须断的时候再断开(虽然并不知道为什么). 如果一段序列满足题目中的性 ...
随机推荐
- 【转】debian下的update-rc.d的使用
在Linux系统下,一个Services的启动.停止以及重启通常是通过/etc/init.d目录下的脚本来控制的.然而,在启动或改变运行级别时, 是在/etc/rcX.d中来搜索脚本.其中X是运行级别 ...
- 【OneNote】使用线性格式输入数学公式
在OneNote中按Alt+=,就可以开始输入公式. # 对齐公式数组 可以使用@和&来实现,如 \eqarray(x+1&=2@1+2+3+y&=z@3/x&=6)& ...
- C++学习之路(五):复制构造函数与赋值运算符重载
之前没有细想过两者的区别,今天对此进行简要记录,后续完善补充. 复制构造函数是在类对象被创建时调用的,但是赋值运算符是被已经存在的对象调用完成赋值操作. 复制构造函数只在对象实例化时才被调用,即在复制 ...
- PhysX SDK
PhysX SDK https://developer.nvidia.com/physx-sdk NVIDIA PhysX SDK Downloads http://www.nvidia.cn/obj ...
- C基础入门 - 第一章 - C语言绪言
第1章 C语言绪言 1.1 C语言概述 1.1.1 C语言世界 1.1.2 C语言学习, 能当饭吃吗 1.2 开发环境构建 1.2.1 visual studio安装使用 1.2.2 visual s ...
- 排名函数row_number() over(order by)用法
1. 定义 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY [列名]DESC) 是先把[列名]降序排列,再为降序以 ...
- ACE_INET_Addr类 API
ACE_INET_Addr类,在这个ACE_网络框架中,应该是比较重要的辅助类,该类主要封装了C SOCKET 的地址对象,通过外观封装的模式,把struct sockaddr_in封装在内.方便用户 ...
- nginx+php7+mysql 在centos7.3下安装
1.Nginx1.8.1 安装 1)安装 nginx 需要的扩展gcc,pcre-devel,zlib-devel, openssl openssl-devel yum -y install gc ...
- http请求数据的格式
最近看了tinyhttpd的服务器代理,看了看http请求数据包的格式和内容 http请求报包含三个部分: 请求行 + 请求头 + 数据体 请求行包含三个内容 method + request-URI ...
- 微信小程序实战篇-下拉刷新与加载更多
下拉刷新 实现下拉刷新目前能想到的有两种方式 1. 调用系统的API,系统有提供下拉刷新的API接口 2. 监听scroll-view,自定义下拉刷新,还记得scroll-view里面有一个binds ...