Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)

B. Weakened Common Divisor

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output

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.

Examples

input

Copy

317 1815 2412 15

output

Copy

6

input

Copy

210 167 17

output

Copy

-1

input

Copy

590 10845 10575 40165 17533 30

output

Copy

5

Note

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;
}

  

原文地址:https://www.cnblogs.com/luowentao/p/9504293.html

时间: 2024-07-30 23:11:44

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)的相关文章

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching B. Pair of Toys C. Bracket Subsequence D. Array Restoration-区间查询最值(RMQ(ST))

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching 题意就是匹配字符的题目,打比赛的时候没有看到只有一个" * ",然后就写挫了,被hack了,被hack的点就是判一下只有一个" * ". 1 //A 2 #include<iostream> 3 #include<cstdio&g

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 standard input output standard output Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colore

【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] [题解] 求出第一个数对的两个数他们有哪些质因子. 显然用这些质因子去试2..n就可以了. 看哪个可以满足 就输出对应的就可以了. (一开始我求出这个数的所有因子(TLE了)..其实没有必要...因为假设y是x的倍数..然后y满足的话,显然x也能满足要求..所以只要用质因子搞就行了. [代码] #include <bits/stdc++.h> #

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) 题解

真心简单的一场比赛 就是坑比较多(自己太蠢) A是一个水题 3分钟的时候过了 B也是一个比较简单的题 类似的套路见得多了 但是我当时可能比较困 想了一会才想出来 19分钟的时候过掉了 C同样很显然 性质不难发现 我在30分钟的时候通过了pretest 但是由于自己的愚蠢 忘记写了一句话 导致FST了... D本来是一个简单的dp题 但是我一直没往dp上想 在网络流上刚了1h之后终于换了思路 在1:45的时候通过了他 然后就时间不多了 E都没看 就去hack 成功hack了2个之后比赛就结束了 题

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: 1 #include<stdio.h> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 #include<set> 8 #inclu

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) D. Array Restoration

D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Initially there was an array aa consisting of nn integers. Positions in it are numbered from 11 to nn. Exactly qq querie

codeforces cf round#505(based on vk cup 2018 final) C. Plasticine zebra

构造题,把整个串想象成一个环.每次把环断开并反转的时候从切口处看进去的顺序是和刚开始从头到尾的顺序是一样的.于是每次不管如何翻转最后都要找到这个环上最大的连续子段长度 #include<bits/stdc++.h> using namespace std; string s; int main() { cin>>s; int tmp=s.size(); s=s+s; int ans=0; int len=1; for(int i=0;i<(int)s.size()-1;i++

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow+差分标记

题目链接:C. Producing Snow 题意:给两个数组v[N],T[N],v[i]表示第i天造的雪,T[i],表示第i天的温度,一堆雪如果<=T[i],当天就会融完,否则融化T[i],要求输出每天的融雪总量. 题解:我对T数组求个前缀和,就可以二分找到每堆雪在那一天(pos)融化,余下的要加进答案中ans[i],然后用一个an数组在a[i]+1,a[pos]-1,最后求再求一次前缀和. ans[i]再加上an[i]*t[i].每次操作二分logn,N次操作.复杂度O(nlogn) #in

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

A. Tritonic Iridescence 题解:分类讨论.注意题目要求,至少有两种方案. 1 #pragma warning(disable:4996) 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 9 int n, m; 10 stri