1. 程式人生 > >Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最長連續交替序列)

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

all clas 拼接 true 序列 -s light scan inf

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 (1n1500001≤n≤150000) — the number of pairs.

Each of the next nn lines contains two integer values aiai, bibi (2ai,bi21092≤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
3
17 18
15 24
12 15
output Copy
6
input Copy
2
10 16
7 17
output Copy
-1
input Copy
5
90 108
45 105
75 40
165 175
33 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;
}

  

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