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)的更多相关文章

  1. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  2. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  3. 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

  4. 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

    题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...

  5. 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 ...

  6. Codeforces Round# 305 (Div 1)

    [Codeforces 547A] #include <bits/stdc++.h> #define maxn 1000010 using namespace std; typedef l ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 「日常训练」Mike and Feet(Codeforces Round #305 Div. 2 D)

    题意 (Codeforces 548D) 对一个有$n$个数的数列,我们要求其连续$x(1\le x\le n)$(对于每个$x$,这样的连续group有若干个)的最小数的最大值. 分析 这是一道用了 ...

随机推荐

  1. chpasswd 更简单的更改密码的方式

    [root@m01 .ssh]# useradd test[root@m01 .ssh]# echo "test:123"|chpasswd Linux命令:chpasswd 批量 ...

  2. EJB包含哪3种bean

    EJB包含哪3种bean 解答:session bean(会话bean), entity bean(实体bean), message bean(消息bean)

  3. 在线生成条形码的解决方案(39码、EAN-13)

    感谢博主:转自:http://xoyozo.eyuyao.com/blog/barcode.html public partial class ReceivablesFormView : System ...

  4. 搭建一个SSM框架

    刚好学完springMVC,mybatis简单的组合,总结一下 1.创建一个maven项目,引入jar包 一共这么多,除了oracle jar包是我手动添加到lib里,其他都是通过pom.xml引入的 ...

  5. 第十篇:顺序容器vector,deque,list的选用规则

    前言 常见的顺序容器主要有三种 - vector,deque,list.它们实现的功能相差不大,那么实际开发中该如何进行选择呢?本文将为你解答这个问题. 分析 由于这三种容器实现的数据结构原型不同(v ...

  6. CNBlog客户端--项目介绍以及技术选型

    项目背景 由于现在开始在博客园写博客,再加上我是android程序员!所以呢,就自然而然的想到自己开发一个自己认为"美"的客户端!!其实还有个原因就是最近我比较闲!!纯属自己给自己 ...

  7. SQL语法:查询此表有另外一个表没有的数据

    select bei.ExamItem_Code2,*from PeisPatientExamItem ppeijoin PeisPatientFeeItem ppfion ppfi.ID_Patie ...

  8. 显示excel工作簿中所有工作表的名称!!!

    问题描述: 有一个工作簿里边将近二百多个工作表(公司),想统计里边所有公司名称即二百多个工作表的名称. 1.找一个空白工作表,也可以利用原来工作表中不用的列,转到“公式”工具栏,点击名称管理,在跳出来 ...

  9. SpringMVC如何接收json数据

    请求头:Content-Type=application/json数据如: {"mobile":"12345678912","smsContent&q ...

  10. [HackerRank] The Longest Common Subsequence

    This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...