Codeforces Round #305 (Div. 2)
C. Mike and Frog
题意:有一只青蛙和一朵花,分别高度为h1、h2,每浇一次水,h1=(x1*h1+y1)mod m,h2=(x2*h2+y2)mod m。求最少浇多少次后h1=a1,h2=a2?
思路:先遍历m次找到第一次达到a1、a2的次数(若无,则为-1);再找到各自的循环节长度,然后枚举m次循环,判断能否符合题意。注意,可能在y2=0的时候,导致a2只出现一次,后面由于出现0而使得0对任意数取模都为0,则应当特别处理。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int m, a1, x1, y1, a2, x2, y2;
long long h1, h2;
scanf("%d", &m);
scanf("%I64d%d%d%d", &h1, &a1, &x1, &y1);
scanf("%I64d%d%d%d", &h2, &a2, &x2, &y2);
int len1=-, len2=-, st1=-, st2=-;
for (int i = ; i <= * m; i++)
{
h1 = (x1*h1 + y1) % m;
h2 = (x2*h2 + y2) % m;
if (h1 == a1)
{
if (st1 == -) st1 = i;
else if (len1 == -) len1 = i - st1;
}
if (h2 == a2)
{
if (st2 == -) st2 = i;
else if (len2 == -) len2 = i - st2;
}
if (len1 != - && len2 != -) break;
}
if (st1 == - || st2 == -||(len1==-&&len2==-&&st1!=st2)) printf("-1\n");
else
{
bool flag = false;
for (int i = ; i <= m; i++)
{
if (len1 > && len2 > )
{
if ((1ll * st1 + 1ll * i * len1) >= st2 && (1ll * st1 + 1ll * i * len1 - st2) % len2 == )
{
printf("%I64d\n", 1ll * st1 + 1ll * i * len1);
flag = true;
break;
}
}
else if (len1 > )
{
if ((1ll * st1 + 1ll * i * len1) == st2)
{
printf("%I64d\n", 1ll * st1 + 1ll * i * len1);
flag = true;
break;
}
}
else break;
}
if (!flag)
{
for (int i = ; i <= m; i++)
{
if (len1 > && len2 > )
{
if ((1ll * st2 + 1ll * i * len2) >= st1 && (1ll * st2 + 1ll * i * len2 - st1) % len1 == )
{
printf("%I64d\n", 1ll * st2 + 1ll * i * len2);
flag = true;
break;
}
}
else if (len2 > )
{
if ((1ll * st2 + 1ll * i * len2) == st1)
{
printf("%I64d\n", 1ll * st2 + 1ll * i * len2);
flag = true;
break;
}
}
else break;
}
}
if (!flag) printf("-1\n");
} return ;
}
/*
999983
408725 408721
1 1
378562 294895
984270 0
499981500166
*/
/*
16
1 0
2 0
1 2
2 0
-1
*/
D. Mike and Feet
题意:有n只熊,每只熊有一个身高,他们站成一列。问区间长度为i时,所有区间最小值的最大值是多少?
思路:用单调栈分别求出每只熊其向左、向右最远的大于等于它身高的熊的位置。设为Li、Ri,则在区间[Li,Ri]内,熊i的身高是最低的,显然ans[Ri-Li+1]=max(ans[Ri-Li+1],h[i]),显然的,在该区间内,长度为min(i-Li+1,Ri-i+1)~Ri-Li+1的区间长度的最小值就为h[i],而对于该区间内区间长度小于min(i-Li+1,Ri-i+1)的最小值,自然可由那些位置的熊的Lj、Rj来确定。而我们此处设置最大范围为该最小值h[i],那么之后我们可以用大区间的值来更新小区间的值。另做证明:如果len1>len2,但是ans[len1]>ans[len2],说明大区间最小值的最大值比小区间的最小值的最大值还大,说明存在某一个长度为len1的大区间,其最小值为v1,那么显然在该大区间内部去一个长度为len2的小区间,则该小区间内最小值至少大于等于v1。故可用大区间的值来更新小区间的值。
#include<iostream>
#include<stack>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = ;
int v[maxn];
int L_index[maxn];
int R_index[maxn];
int ans[maxn];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", v + i);
stack<int>stkL,stkR;
for (int i = ; i <= n; i++)
{
while (!stkL.empty() && v[i] <= v[stkL.top()]) stkL.pop();
if (stkL.empty()) L_index[i] = ;
else L_index[i] = stkL.top() + ;
stkL.push(i);
}
for (int i = n; i>=; --i)
{
while (!stkR.empty() && v[i] <= v[stkR.top()]) stkR.pop();
if (stkR.empty()) R_index[i] = n;
else R_index[i] = stkR.top() - ;
stkR.push(i);
}
for (int i = ; i <= n; i++)
{
int len = R_index[i] - L_index[i] + ;
ans[len] = max(ans[len], v[i]);
}
for (int i = n-; i >= ; --i)
{
ans[i] = max(ans[i], ans[i + ]);
}
for (int i = ; i <= n; i++)
{
if (i > ) printf(" ");
printf("%d", ans[i]);
}
printf("\n"); return ;
}
E. Mike and Foam
题意:有n杯啤酒,每杯啤酒的泡沫含量为ai。现在有一个空架子,q次询问,每次询问一杯啤酒的编号,如果其在架子上,则将其拿下,否则将其放上。并在每次询问后求架子上泡米含量互质的啤酒的对数。
思路:先求出含量ai其所有的质因子。然后求所有质因子的并集(容斥原理),得到与ai不互质的个数,然后架子上的啤酒个数减去ai就是与ai互质的对数。并且我们每次询问可以累积这个对数,这样每次无论是放上还是取下啤酒,都只需找到与进行操作的啤酒的互质的个数即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxv = ;
const int maxn = ;
int v[maxn];
bool isOnShelf[maxn];
vector<int>p[maxv];//存放2~maxv各个数的质因子
int n, q;
int sets[maxv];//存放各个质因子及其乘积的个数
int preCount;//之前架子上的及啤酒个数
long long pre;//之前架子上互质的对数
void Init()
{
for (int i = ; i < maxv; i++)
{
if (p[i].empty())
{
for (int j = i; j < maxv; j += i)
{
p[j].push_back(i);
}
}
}
} void removeBeer(int x)
{
int len = p[x].size();
int tot = ( << len) - ;
int tans = ;
//二进制枚举质因子个数,进行容斥,找到已有数的集合中与x不互质的个数(也就是含有至少一个相同质因子的个数,p1,p2,...,p_len的并集)
for (int i = ; i <= tot; i++)
{
//得到当前状态下各质因子的乘积及其个数
int cur = ,nums=;
for (int j = ; j < len; j++)
{
if (( << j)&i)
{
cur *= p[x][j], nums++;
}
}
//在cur的集合中减去一个数(即x自身的若干质因子的乘积),因为要把x从当前数的集合减去
sets[cur]--;
//奇加偶减
if (nums % ) tans += sets[cur];
else tans -= sets[cur];
}
preCount--;//减去x自身
pre -= (preCount - tans);
}
void addBeer(int x)
{
int len = p[x].size();
int tot = ( << len) - ;
int tans = ;
//二进制枚举质因子个数,进行容斥,找到已有数的集合中与x不互质的个数(也就是含有至少一个相同质因子的个数,p1,p2,...,p_len的并集)
for (int i = ; i <= tot; i++)
{
//得到当前状态下各质因子的乘积及其个数
int cur = , nums = ;
for (int j = ; j < len; j++)
{
if (( << j)&i)
{
cur *= p[x][j], nums++;
}
}
//奇加偶减
if (nums % ) tans += sets[cur];
else tans -= sets[cur];
//在cur的集合中加去一个数(即x自身的若干质因子的乘积),因为要把x放入当前数的集合
sets[cur]++;
}
pre += (preCount - tans);
preCount++;//加上x自身
}
int main()
{
Init();
scanf("%d%d", &n, &q);
for (int i = ; i <= n; i++) scanf("%d", v + i);
while (q--)
{
int index;
scanf("%d", &index);
if (isOnShelf[index])
{
removeBeer(v[index]);
printf("%I64d\n", pre);
isOnShelf[index] = false;
}
else
{
addBeer(v[index]);
printf("%I64d\n", pre);
isOnShelf[index] = true;
}
}
return ;
}
Codeforces Round #305 (Div. 2)的更多相关文章
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力
B. Mike and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...
- Codeforces Round# 305 (Div 1)
[Codeforces 547A] #include <bits/stdc++.h> #define maxn 1000010 using namespace std; typedef l ...
- Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串
A. Mike and Fax Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...
- Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈
B. Mike and Feet Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...
- Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力
A. Mike and Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...
- 「日常训练」Mike and Feet(Codeforces Round #305 Div. 2 D)
题意 (Codeforces 548D) 对一个有$n$个数的数列,我们要求其连续$x(1\le x\le n)$(对于每个$x$,这样的连续group有若干个)的最小数的最大值. 分析 这是一道用了 ...
随机推荐
- [Tips] bzr Import error
# bazzar error on Mac Marvericks: 192:~ piaoger$ bzrbzr: ERROR: Couldn't import bzrlib and dependenc ...
- 几款 ping tcping 工具总结
本文转载至:http://www.cnblogs.com/kerrycode/p/8092942.html ping 命令以前是一个很好用并且常用的网络测试工具,它是基于 ICMP 协议,但是出于网络 ...
- smartJS 0.1 API 讲解 - PromiseEvent
上篇简单的介绍smartjs了一些通用方法的api.这篇介绍基础的PromiseEvent(这个名字一直没想好,以前准备用callbacks的,但避免与jquery混淆,st的命名空间可以直接挂到$上 ...
- jqgrid的排序问题
今天是本人解决的一个小bug 所以写的粗略些. 问题是这样的ORDER BY a.$sidx $sord 当时本人排序时候没用jqgrid的默认排序(可能今天这个大家看不懂,很抱歉啊各位,今天主要 ...
- (转)TCP三次握手四次挥手
转自:http://www.jellythink.com/archives/705 参考:http://blog.csdn.net/whuslei/article/details/6667471 [注 ...
- chrome vim设置
chrome vi 插件:Vrome 配置文件如下 set hintkeys=asdfghjkl;" 把默认使用数字来完成提示改为使用键盘上的这几个键map j 10jmap k 10k&q ...
- iOS-去除NavigationBar边线
解决办法: self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
- 【Cygwin】Windows下使用linux命令
我参阅了这份文章: 让windows cmd也用上linux命令 原文时间有点久了,Cygwin也更新了... 所以我的做法简单了很多... 到Cygwin官网下载安装包:https://cygwin ...
- tfs+git
TFS+GIT 一:背景介绍 技术团队的代码管理工具原来使用的是纯TFS方案,使用两年后发现一些问题:体积太大,每次新建一个分支需要本地下载一份代码:操作不便,功能分支的建立.合并不方便,本地有很多同 ...
- 【BZOJ4385】[POI2015]Wilcze doły 单调栈+双指针法
[BZOJ4385][POI2015]Wilcze doły Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段 ...