Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)
1.5 seconds
256 megabytes
standard input
standard output
During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers.
For a given list of pairs of integers (a1,b1)(a1,b1), (a2,b2)(a2,b2), ..., (an,bn)(an,bn) their WCD is arbitrary integer greater than 11, such that it divides at least one element in each pair. WCD may not exist for some lists.
For example, if the list looks like [(12,15),(25,18),(10,24)][(12,15),(25,18),(10,24)], then their WCD can be equal to 22, 33, 55 or 66 (each of these numbers is strictly greater than 11 and divides at least one number in each pair).
You're currently pursuing your PhD degree under Ildar's mentorship, and that's why this problem was delegated to you. Your task is to calculate WCD efficiently.
The first line contains a single integer nn (1≤n≤1500001≤n≤150000) — the number of pairs.
Each of the next nn lines contains two integer values aiai, bibi (2≤ai,bi≤2⋅1092≤ai,bi≤2⋅109).
Print a single integer — the WCD of the set of pairs.
If there are multiple possible answers, output any; if there is no answer, print −1−1.
3
17 18
15 24
12 15
6
2
10 16
7 17
-1
5
90 108
45 105
75 40
165 175
33 30
5
In the first example the answer is 66 since it divides 1818 from the first pair, 2424 from the second and 1212 from the third ones. Note that other valid answers will also be accepted.
In the second example there are no integers greater than 11 satisfying the conditions.
In the third example one of the possible answers is 55. Note that, for example, 1515 is also allowed, but it's not necessary to maximize the output.
http://codeforces.com/contest/1025/problem/B
大意:求出weakened common divisor (WCD)
给出n对数,他们的WCD为可以将每一对数中至少一个数整除的数(1除外),如果不存在则输出-1.存在多个则输出任意一个。
两种方法:
1.将第一对数保留(a,b),后面每一对数相乘,变成一个数x。然后更新(a,b),将其替换成gcd(a,x),gcd(b,x)。最后再输出a或者b的最小因子
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a,b,n;
ll gcd(ll x,ll y){return x%y? gcd(y,x%y):y;}
int main(){
cin>>n;
cin>>a>>b;
for(int i=1;i<n;++i){
ll x,y;cin>>x>>y;
x*=y;a=gcd(a,x);b=gcd(b,x);
}
if(a==1&&b==1){cout<<"-1\n";return 0;}
for(ll i=2;i*i<=a||i*i<=b;++i)
if(a%i==0||b%i==0){cout<<i<<endl;return 0;}
if(a!=1)cout<<a<<endl;
else cout<<b<<endl;
return 0;
}
方法2.
把第一对数的所有因子提出来然后对后面的每一对数进行测试,如果后面每一对数中只是有一个可以被因子可以整除,则输出因子。
代码:
#include<bits/stdc++.h>
int n,a[150010],b[150010],p[100],c;
void d(int x){
for(int i=2;1ll*i*i<=x;i++)if(x%i==0){
p[c++]=i;
while(x%i==0)x/=i;
}
if(x>1)p[c++]=x;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d%d",a+i,b+i);
d(a[0]);
d(b[0]);
for(int i=0;i<c;i++){
bool fl=1;
for(int j=0;j<n&&fl;j++)if(a[j]%p[i]!=0&&b[j]%p[i]!=0)fl=0;
if(fl){
printf("%d\n",p[i]);
return 0;
}
}
puts("-1");
}
C. Plasticine zebra
http://codeforces.com/contest/1025/problem/C
题目大意:输出一个字符串中最长“斑马条纹”的长度,“斑马条纹”指类似于“wbwbwbw”(7)、“bwbwb”(5)。
同时为了最终获得更长的长度,可以在组装斑马之前,Grisha可以进行以下操作0或更多次。他将序列在某个地方分成两部分,然后将它们中的每一部分反转并再次将它们粘在一起。例如,如果Grisha的顺序为“ bwbbw ”(这里' b '表示黑条,' w '表示白条),那么他可以将序列拆分为bw | bbw(此处垂直条代表切割),反转两个部分并获得“ wbwbb ”。
例如
input:
bwwwbwwbw
output:
5
备注:在第一个例子中,可能的操作顺序之一是bwwbww | bw → w | wbwwwbwb → wbwbw wwbw,给出的答案等于5。
思路:
从样例一中逆推,发现最后得到的斑马条纹其实始终来自序列的两侧,经过操作后拼接在一起,于是我们可以直接将两个s拼接在一起,创造出一个2s,此时找出2s中的最长斑马条纹即可。
但要注意最长长度不会超过s的长度,就错在这里没有过fst。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int maxn=1e7+50;
const ll inf=0x3f3f3f3f3f3f;
int k;
ull gcd(ull a,ull b)
{
while(b)
{
int t=a%b;
a=b;
b=t;
}
return a;
}
ull lcm(ull a,ull b){
return a/gcd(a,b)*b;
}
ull sum[maxn];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
string s;
int num=1,maxnum=0;
cin>>s;
s=s+s;
for(int i=1;i<s.size();i++)
{
if(s[i]!=s[i-1])
{
num++;
}
else{
num=1;
}
if(num>maxnum)
{
maxnum=num;
}
}
int k=s.size();
cout<<min(maxnum,k/2)<<endl;
return 0;
}
Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)的更多相关文章
- D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B. Weakened Common Divis
题目链接 让你找一个数,使得这个数,可以被每个二元组的两个数中的一个数整除. 先将第一个二元组的两个数质因数分解一下,分解的质数加入set中,然后,对剩下的n-1个二元组进行遍历,每次遍历到的二元组对 ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
A : A. Doggo Recoloring time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Plasticine zebra
问了学长,感觉还是很迷啊,不过懂了个大概,这个翻转操作,实质不就是在序列后面加上前面部分比如 bw | wwbwwbw 操作过后 wbwbwwbww 而 bw | wwbwwbwbw 这样我们就知道 ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) 题解
真心简单的一场比赛 就是坑比较多(自己太蠢) A是一个水题 3分钟的时候过了 B也是一个比较简单的题 类似的套路见得多了 但是我当时可能比较困 想了一会才想出来 19分钟的时候过掉了 C同样很显然 性 ...
- 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) C】
[链接] 我是链接,点我呀:) [题意] 给你一个字符串s. 让你在其中的某一些位置进行操作.. 把[1..i]和[i+1..n]翻转. 使得里面01交替出现的那种子串的长度最长. [题解] 可以用a ...
- 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A】 Doggo Recoloring
[链接] 我是链接,点我呀:) [题意] 你可以把出现次数大于1的颜色换成其他颜色. 问你最后能不能全都变成同一种颜色 [题解] 判断一下有没有出现次数大于1的就好. 有的话.显然可以一直用它变颜色. ...
- 【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor
[链接] 我是链接,点我呀:) [题意] 给你n个数对(ai,bi). 让你求一个大于1的数字x 使得对于任意的i x|a[i] 或者 x|b[i] [题解] 求出第一个数对的两个数他们有哪些质因子. ...
- E - Down or Right Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
http://codeforces.com/contest/1023/problem/E 交互题 #include <cstdio> #include <cstdlib> #i ...
随机推荐
- Django笔记 —— 视图
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- 最近做group assignment需要些加密的知識
需求:A給B單向發的數據需要被加密,A和B都可以看到原文.加密后,就算傳輸的過程被竊取,也無法得知數據原文.A可以是任何客戶端. 解決:常用的MD5,sha1等常用的加密算法為單向不可逆,顯然不符合需 ...
- 性能测试之siege
一.siege介绍 Siege是一个压力测试和评测工具,设计用于WEB开发这评估应用在压力下的承受能力:可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数 ...
- mongoDB坑
1 mongodb.cnf文件中有个选项为bind_id:127.0.0.1,如果是测试环境,需要远程访问的话,就先改成0.0.0.1 auth:如果只是学习的话,建议先改成false,否则后面会有各 ...
- python学习总结---函数使用 and 生成器
# 函数使用 ### 生成器 - 使用场景 在使用列表时,很多时候我们不会一下子使用全部数据,通常都是一个一个使用,但是当数据量比较大的时候,定义一个大的列表将会是内容使用突然增大.为了解决此类问题, ...
- (原)Unreal 渲染模块 渲染流程
@author:白袍小道 浏览分享随缘,评论不喷亦可. 扯淡部分: 在temp中,乱七八糟的说了下大致的UE过程.下面我们还是稍微别那么任性,一步步来吧. UE渲染模块牵扯到场景遍历. ...
- lshw
https://linux.die.net/man/1/lshw lshw(Hardware Lister)是另外一个可以查看硬件信息的工具,不仅如此,它还可以用来做一些硬件的benchmark. 这 ...
- 聊聊、Spring WebApplicationInitializer
说到 WebApplicationInitializer,这个接口是为了实现代码配置 Web 功能.只要实现了这个接口,那么就可以实现 Filter,Servlet,Listener 等配置,跟在 x ...
- java中从实体类中取值会忽略的的问题
在我们java Map中通过get来取值时会忽略的问题是:如果取得一个空值null时,那么.toString()时就会出错,而且不知道是什么原因. 现在我给的具体方法是用条件表达式先判断一下. 例: ...
- CodeForces Round #515 Div.3 C. Books Queries
http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...