Codeforces Round #305 (Div. 1) A B C

2024-01-28 06:08
文章标签 codeforces round div 305

本文主要是介绍Codeforces Round #305 (Div. 1) A B C,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        这场比赛爆零了,感觉自己还是弱。。而且有点过于care rating了。做比赛还开小号,不敢用大号。根本没有攻克难题的魄力和勇气。

        以后只打一个号就好了,实力增强了总是能涨的。这场的题应该还是偏简单的,比赛的时候还是各种2各种想不出,感谢round305暴露了我很多弱点。


A Mike and Frog

        每过一秒,h1=(x1*h1+y1)%m,h2=(x2*h2+y2)%m。求使h1=a1,h2=a2同时满足的最早时刻。

        找到使h1第一次等于a1的时间,再找使它循环的时间,对h2同理。然后弄到set里面乱搞就好了。更科学的做法应该是扩展欧几里德。

#include <bits/stdc++.h>  
using namespace std;#define ll long long
const int INF = 1e9;int main(){ll m,h1,a1,x1,y1;ll   h2,a2,x2,y2;cin>>m;cin>>h1>>a1;cin>>x1>>y1;cin>>h2>>a2;cin>>x2>>y2;//ll ans1=1;for(;ans1<=m+1;ans1++){h1=x1*h1+y1;h1%=m;if(h1==a1){break;}}ll ans2=1;for(;ans2<=m+1;ans2++){h2=x2*h2+y2;h2%=m;if(h2==a2){break;}}ll cyc1=1;for(;cyc1<=m+1;cyc1++){h1=x1*h1+y1;h1%=m;if(h1==a1){break;}}ll cyc2=1;for(;cyc2<=m+1;cyc2++){h2=x2*h2+y2;h2%=m;if(h2==a2){break;}}bool ok=1;if(ans1>m+1||ans2>m+1)ok=0;if(ok){if(cyc1>m+1)cyc1=0;if(cyc2>m+1)cyc2=0;set<ll> s;for(int i=0;i<=m;i++){s.insert(ans1+cyc1*i);}for(int i=0;i<=m;i++){if(s.count(ans2+cyc2*i)){cout<<ans2+cyc2*i<<endl;return 0;}}cout<<-1;}else{cout<<-1;}return 0;
}      


B Mike and Feet

        长度为n的数列,求连续长度x=1~n的一串数中,最小值的最大值。

        维护一个单调栈,使值递增。出栈时维护长度并尝试更新答案,具体见代码。

#include <bits/stdc++.h>  
using namespace std;#define ll long long
const int INF = 1e9;struct node{int val;int len;node(int val,int len):val(val),len(len){}node(){}
};
stack<node> sta;int a[200010];
int ans[200010];int main(){int n;cin>>n;for(int i=1;i<=n;i++){scanf("%d",&a[i]);int curlen=0;while(sta.size() && sta.top().val>=a[i]){int val=sta.top().val;curlen+=sta.top().len;sta.pop();ans[curlen]=max(ans[curlen],val);}sta.push(node(a[i],curlen+1));}int curlen=0;while(sta.size()){int val=sta.top().val;curlen+=sta.top().len;sta.pop();ans[curlen]=max(ans[curlen],val);}for(int i=n;i>=1;i--){if(ans[i]<ans[i+1])ans[i]=ans[i+1];}for(int i=1;i<=n;i++){printf("%d ",ans[i]);}cout<<endl;return 0;
} 

        另外我还看到了一个使用并查集的做法。先排序,从大到小标记元素,并查集维护当前元素与左右已被标记的元素连起来的最大长度。


C Mike and Foam

        有n个数,初始均为禁用状态。有q个询问,每次改变一个数的状态(禁用<-->启用)。问启用的那些数里面,有多少对数满足最大公约数为1。

        这是我第一道用容斥原理做的题。。本来没想法,看了题解后发现非常精妙。首先把1~500000的所有素因子筛出来(注意到每个数最多有6个不同的素因子)。假设新增或删除的数与其他启用的数最大公约数都等于1,那么新增或删除后,答案的改变量就是除了它之外,启用的数的个数(新增加上,删除减去)。但是,新增或删除的那个数,可能与某些数的最大公约数不为1,这就需要用容斥原理来处理。比如ai共有6个素因子,那么就用6位二进制数把它的素因子的所有子集表示出来,为所有素因子集合计数,维护答案。

#include <bits/stdc++.h>  
using namespace std;#define ll long long
const int INF = 1e9;const int maxn =200010;
const int maxa =500000;int a[maxn];
bool state[maxn];vector<int> factors[maxa+1];
int cnt[maxa+1];	//统计素因子的集合 void init(){for(int i=2;i<=500000;i++){if(!factors[i].size()){for(int j=i;j<=500000;j+=i){factors[j].push_back(i);}}}
}static ll ans=0;
static int tot=0;void fun(int num,int flag){int t=1<<factors[num].size();ans+=tot*flag;//枚举子集 for(int i=1;i<t;i++){int tmp=1;	//num素因子的子集中,所有元素的积。 int bit=0;	//num素因子的子集中,有多少个元素。for(int j=0;j<factors[num].size();j++){if(i&(1<<j)){tmp*=factors[num][j];bit++;}}if(flag==-1)cnt[tmp]+=flag;int one;if(bit&1)one=1;else one=-1;		if(cnt[tmp]){//容斥维护答案,加多了减去,减多了加上。。。 ans-=cnt[tmp]*flag*one;}if(flag==1)cnt[tmp]+=flag;}
}int main(){init();int n,q;cin>>n>>q;for(int i=1;i<=n;i++){scanf("%d",&a[i]);}int x;for(int i=1;i<=q;i++){scanf("%d",&x);if(state[x]){tot--;state[x]=0;fun(a[x],-1);}else{state[x]=1;fun(a[x],1);tot++;}printf("%I64d\n",ans);}return 0;
}


这篇关于Codeforces Round #305 (Div. 1) A B C的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/652692

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There