Codeforces Round #609 (Div. 2)

A. Equation

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s call a positive integer composite if it has at least one divisor other than 1 and itself. For example:

the following numbers are composite: 1024, 4, 6, 9;
the following numbers are not composite: 13, 1, 2, 3, 37.
You are given a positive integer n. Find two composite integers a,b such that a−b=n.

It can be proven that solution always exists.

Input
The input contains one integer n (1≤n≤107): the given integer.

Output
Print two composite integers a,b (2≤a,b≤109,a−b=n).

It can be proven, that solution always exists.

If there are several possible solutions, you can print any.

Examples
inputCopy
1
outputCopy
9 8
inputCopy
512
outputCopy
4608 4096

#include
using namespace std;
int t,n;
int main()
{
    cin>>n;
    if(n==1)cout<<9<<" "<<8<<endl;
    else
    cout<<3*n<<" "<<2*n<<endl;
    return 0;
}

B. Modulo Equality

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a positive integer m and two integer sequence: a=[a1,a2,…,an] and b=[b1,b2,…,bn]. Both of these sequence have a length n.

Permutation is a sequence of n different positive integers from 1 to n. For example, these sequences are permutations: [1], [1,2], [2,1], [6,7,3,4,1,2,5]. These are not: [0], [1,1], [2,3].

You need to find the non-negative integer x, and increase all elements of ai by x, modulo m (i.e. you want to change ai to (ai+x)modm), so it would be possible to rearrange elements of a to make it equal b, among them you need to find the smallest possible x.

In other words, you need to find the smallest non-negative integer x, for which it is possible to find some permutation p=[p1,p2,…,pn], such that for all 1≤i≤n, (ai+x)modm=bpi, where ymodm — remainder of division of y by m.

For example, if m=3, a=[0,0,2,1],b=[2,0,1,1], you can choose x=1, and a will be equal to [1,1,0,2] and you can rearrange it to make it equal [2,0,1,1], which is equal to b.

Input
The first line contains two integers n,m (1≤n≤2000,1≤m≤109): number of elemens in arrays and m.

The second line contains n integers a1,a2,…,an (0≤ai

The third line contains n integers b1,b2,…,bn (0≤bi

It is guaranteed that there exists some non-negative integer x, such that it would be possible to find some permutation p1,p2,…,pn such that (ai+x)modm=bpi.

Output
Print one integer, the smallest non-negative integer x, such that it would be possible to find some permutation p1,p2,…,pn such that (ai+x)modm=bpi for all 1≤i≤n.

Examples
inputCopy
4 3
0 0 2 1
2 0 1 1
outputCopy
1
inputCopy
3 2
0 0 0
1 1 1
outputCopy
1
inputCopy
5 10
0 0 0 1 2
2 1 0 0 0
outputCopy
0

#include
using namespace std;
int t,n,m,mia,mib,s1,s2;
int a[2010];
int b[2010];
vector<int>v[2010];
vector<int>k[2010];
bool check(int x)
{
    vector<int>c;
    for(auto i:v[mia])
    {
        int f=(i+x)%m;
        c.push_back(f);
    }
    sort(c.begin(),c.end());
    if(c==k[mib])return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(a,-1,sizeof(a));memset(b,-1,sizeof(b));
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    sort(b+1,b+1+n);
    sort(a+1,a+1+n);
    t=1;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==a[i+1])t++;
        else
        {
            v[t].push_back(a[i]);
            t=1;
        }
    }
    t=1;
    for(int i=1;i<=n;i++)
    {
        if(b[i]==b[i+1])t++;
        else
        {
            k[t].push_back(b[i]);
            t=1;
        }
    }
    mia=1,mib=1;s1=v[1].size(),s2=k[1].size();
    for(int i=2;i<=2000;i++)
    {
        if(s1==0||(v[i].size()<s1&&v[i].size()>0))
        {
            mia=i;s1=v[i].size();
        }
        if(s2==0||(k[i].size()<s2&&k[i].size()>0))
        {
            mib=i;s2=k[i].size();
        }
    }
    vector<int>bb;
    for(int i=0;i<s1;i++)
    {
        if(k[mib][i]>=v[mia][0])
        bb.push_back(k[mib][i]);
        if(k[mib][i]+m>=v[mia][0])
        bb.push_back(k[mib][i]+m);
    }
    sort(bb.begin(),bb.end());
    int len=bb.size();
    for(int i=0;i<len;i++)
    if(check(bb[i]-v[mia][0])==true)
    {
        cout<<bb[i]-v[mia][0]<<endl;return 0;
    }
    return 0;
}

C. Long Beautiful Integer

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an integer x of n digits a1,a2,…,an, which make up its decimal notation in order from left to right.

Also, you are given a positive integer k

Let’s call integer b1,b2,…,bm beautiful if bi=bi+k for each i, such that 1≤i≤m−k.

You need to find the smallest beautiful integer y, such that y≥x.

Input
The first line of input contains two integers n,k (2≤n≤200000,1≤k

The next line of input contains n digits a1,a2,…,an (a1≠0, 0≤ai≤9): digits of x.

Output
In the first line print one integer m: the number of digits in y.

In the next line print m digits b1,b2,…,bm (b1≠0, 0≤bi≤9): digits of y.

Examples
inputCopy
3 2
353
outputCopy
3
353
inputCopy
4 2
1234
outputCopy
4
1313

#include
using namespace std;
int t,n,k;
string s1,s;
char c[200010];
int main()
{
    cin>>n>>k;
    s="v";
    string a;
    cin>>s1;
    s+=s1;a=s;
    int len=n-k;
    for(int i=1;i<=len;i++)
    {
        a[i+k]=a[i];
    }
    while(a<s)
    {
        if(a[k]<'9')a[k]++;
        else
        {int f=k;
            while(a[f]=='9'&&f>1)--f;
            a[f]++;
            for(int i=f+1;i<=k;i++)a[i]='0';
            for(int i=1;i<=len;i++)a[i+k]=a[i];
        }
        for(int i=1;i<=len;i++)a[i+k]=a[i];
    }
    cout<<n<<endl;
    a.erase(0,1);cout<<a<<endl;
    return 0;
}

D. Domino for Young

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a Young diagram.

Given diagram is a histogram with n columns of lengths a1,a2,…,an (a1≥a2≥…≥an≥1).

Young diagram for a=[3,2,2,2,1].
Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1×2 or 2×1 rectangle.

Input
The first line of input contain one integer n (1≤n≤300000): the number of columns in the given histogram.

The next line of input contains n integers a1,a2,…,an (1≤ai≤300000,ai≥ai+1): the lengths of columns.

Output
Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram.

Example
inputCopy
5
3 2 2 2 1
outputCopy
4
Note
Some of the possible solutions for the example:

#include
using namespace std;
int n;
long long a[300010];
int main()
{
    cin>>n;long long ans=0;
    int v1=0,v2=0;
    for(int i=1;i<=n;i++)
    {cin>>a[i];ans+=a[i]/2;}
    for(int i=1;i<=n;i++)
    {
        if(a[i]&1)if(i&1) v1++;else v2++;
    }
    ans+=min(v1,v2);
    cout<<ans<<endl;
    return 0;
}

你可能感兴趣的:(codeforces)