Codeforces 360Div2

22 篇文章 0 订阅
20 篇文章 0 订阅

A. Opponents


time limit per test 1 secondmemory limit per test 256 megabytes

Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya’s opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya.

For each opponent Arya knows his schedule — whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents.

Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents.

Input

The first line of the input contains two integers n and d (1 ≤ n, d ≤ 100) — the number of opponents and the number of days, respectively.

The i-th of the following d lines contains a string of length n consisting of characters ‘0’ and ‘1’. The j-th character of this string is ‘0’ if the j-th opponent is going to be absent on the i-th day.

Output

Print the only integer — the maximum number of consecutive days that Arya will beat all present opponents.

Examples
input

2 2
10
00

output

2

input

4 1
0100

output

1
input

4 5

1101
1111
0110
1011
1111

output

2

Note

In the first and the second samples, Arya will beat all present opponents each of the d days.

In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4.

题意:判断字符串不全是1的连续的最长的长度


#include <bits/stdc++.h>

using namespace std;

int n,m;

char s[110];

int main()
{
    cin>>n>>m;

    int ans = 0;

    int sum = 0;

    for(int i = 0;i<m;i++)
    {
        scanf("%s",s);


        bool flag = false;

        for(int j = 0 ;j<n;j++)
        {
            if(s[j]!='1')
            {
                flag = true;

                break;
            }
        }

        if(!flag)
        {
            sum = 0 ;
        }
        else 
        {
            sum++;

            ans = max(ans,sum);
        }

    }

    cout<<ans<<endl;

    return 0;
}

B. Lovely Palindromes


time limit per test 1 secondmemory limit per test 256 megabytes

Pari has a friend who loves palindrome numbers. A palindrome number is a number that reads the same forward or backward. For example 12321, 100001 and 1 are palindrome numbers, while 112 and 1021 are not.

Pari is trying to love them too, but only very special and gifted people can understand the beauty behind palindrome numbers. Pari loves integers with even length (i.e. the numbers with even number of digits), so she tries to see a lot of big palindrome numbers with even length (like a 2-digit 11 or 6-digit 122221), so maybe she could see something in them.

Now Pari asks you to write a program that gets a huge integer n from the input and tells what is the n-th even-length positive palindrome number?

Input

The only line of the input contains a single integer n (1 ≤ n ≤ 10100 000).

Output

Print the n-th even-length palindrome number.

Examples
input

1

output

11

input

10

output

1001

Note

The first 10 even-length palindrome numbers are 11, 22, 33, … , 88, 99 and 1001.

题意:给你一个数n,问第n个偶数的回文数是多少,正着输出,然后倒着输出



#include <bits/stdc++.h>

using namespace std;

char s[100100];

int main()
{
    scanf("%s",s);

    int len = strlen(s);

    printf("%s",s);
    for(int  i = len-1;i>=0;i--)
    {
        printf("%c",s[i]);
    }

    printf("\n");

    return 0;
}

C. NP-Hard Problem


time limit per test 2 secondsmemory limit per test 256 megabytes

Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.

Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).

Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.

They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it’s impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.

Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It’s guaranteed the graph won’t contain any self-loops or multiple edges.

Output

If it’s impossible to split the graph between Pari and Arya as they expect, print “-1” (without quotes).

If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Examples
input

4 2
1 2
2 3

output

1
2
2
1 3

input

3 3
1 2
2 3
1 3

output

-1

Note

In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.

题意:给你一个图,判断能不能分成二分图。二分染色



#include <bits/stdc++.h>

using namespace std;

vector<int>E[100100];

int n,m;

int u,v;

bool vis[110110];

vector<int> ans[2];

int type[110100];

bool  op ;

void dfs(int u,int fa,int st)
{
    vis[u] = true;

    ans[st].push_back(u);

    type[u] = st;

    for(int i = 0;i<E[u].size();i++)
    {
        if(E[u][i] == fa) continue;

        if(vis[E[u][i]])
        {
            if(type[u] == type[E[u][i]])
            {
                op = true;

                return ;
            }
            else continue;
        }

        dfs(E[u][i],u,(st+1)%2);
    }
}

int main()
{
    scanf("%d %d",&n,&m);

    for(int i = 0;i<m;i++)
    {
        scanf("%d %d",&u,&v);

        E[u].push_back(v);

        E[v].push_back(u);
    }

    memset(vis,false,sizeof(vis));

    memset(type,-1,sizeof(type));

    op  = false;

    for(int i = 1;i<=n;i++)
    {
        if(op) break;

        if(!vis[i])
        {
            dfs(i,-1,0);
        }
    }

    if(op) printf("-1\n");
    else
    {
        printf("%d\n",ans[0].size());

        for(int i = 0;i<ans[0].size();i++)
        {
            if(i) printf(" ");

            printf("%d",ans[0][i]);
        }
        printf("\n");
        printf("%d\n",ans[1].size());

        for(int i = 0;i<ans[1].size();i++)
        {
            if(i) printf(" ");

            printf("%d",ans[1][i]);
        }
        printf("\n");
    }
    return 0;
}

D. Remainders Game


time limit per test 1 secondmemory limit per test 256 megabytes

Today Pari and Arya are playing a game called Remainders.

Pari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value . There are n ancient numbers c1, c2, …, cn and Pari has to tell Arya if Arya wants. Given k and the ancient values, tell us if Arya has a winning strategy independent of value of x or not. Formally, is it true that Arya can understand the value for any positive integer x?

Note, that means the remainder of x after dividing it by y.

Input

The first line of the input contains two integers n and k (1 ≤ n,  k ≤ 1 000 000) — the number of ancient integers and value k that is chosen by Pari.

The second line contains n integers c1, c2, …, cn (1 ≤ ci ≤ 1 000 000).

Output

Print “Yes” (without quotes) if Arya has a winning strategy independent of value of x, or “No” (without quotes) otherwise.

Examples
input

4 5
2 3 5 12

output

Yes

input

2 7
2 3

output

No

Note

In the first sample, Arya can understand because 5 is one of the ancient numbers.

In the second sample, Arya can’t be sure what is. For example 1 and 7 have the same remainders after dividing by 2 and 3, but they differ in remainders after dividing by 7.

题意:给你k和n,你现在想要知道x,但是人家不告诉你,给你n个ci,表示你可以知道x%ci,问你能不能唯一确定x

思路:首先,根据剩余定理,如果我们想知道x%m等于多少,当且仅当我们知道x%m1,x%m2..x%mr分别等于多少,其中m1m2...mr=m,并且mi相互互质,即构成独立剩余系。令m的素数分解为m=p1^k1p2^k2...pr^kr,如果任意i,都有pi^ki的倍数出现在集合中,那么m就能被猜出来。这个问题等价于问LCM(ci)%m是否等于0所以只要求出LCM(ci)即可,不过要边求lcm,边和m取gcd,防止爆int



#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL n,m,lcm,k,c;

LL GCD(LL a,LL b)
{
    return b == 0?a:GCD(b,a%b);
}
int main()
{
    ios_base::sync_with_stdio(false);

    cin>>n>>m;

    lcm = 1;

    for(int i = 0;i<n;i++)
    {
        cin>>c;

        if(!lcm) continue;

        lcm = (c*lcm)/GCD(lcm,c);

        lcm%=m;
    }
    if(lcm) cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
    return 0;
}

E. The Values You Can Make


time limit per test 2 secondsmemory limit per test 256 megabytes

Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.

Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn’t want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.

Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.

Input

The first line contains two integers n and k (1  ≤  n, k  ≤  500) — the number of coins and the price of the chocolate, respectively.

Next line will contain n integers c1, c2, …, cn (1 ≤ ci ≤ 500) — the values of Pari’s coins.

It’s guaranteed that one can make value k using these coins.

Output

First line of the output must contain a single integer q— the number of suitable values x. Then print q integers in ascending order — the values that Arya can make for some subset of coins of Pari that pays for the chocolate.

Examples
input

6 18
5 6 1 10 12 2

output

16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18

input

3 50
25 25 50

output

3
0 25 50

题意:给你n枚硬币和相应的价值,判断组成k的硬币能够组成的面值。

Dp[i][j][k]ijk10



#include <bits/stdc++.h>

using namespace std;

int n,m,s;

bool dp[510][510][510];

vector<int>ans;

int main()
{
    scanf("%d %d",&n,&m);

    dp[0][0][0] = 1;

    for(int i = 1;i<=n;i++)
    {
        scanf("%d",&s);

        for(int j = 0;j<=m;j++)
        {
            for(int k = 0;k<=j;k++)
            {
                dp[i][j][k] = dp[i-1][j][k];

                if(j>=s) dp[i][j][k] |= dp[i-1][j-s][k];

                if(k>=s) dp[i][j][k] |= dp[i-1][j-s][k-s];
            }
        }

    }

    for(int i =0;i<=500;i++)
    {
        if(dp[n][m][i]) ans.push_back(i);
    }

    printf("%d\n",ans.size());

    for(int i = 0;i<ans.size();i++)
    {
        if(i) printf(" ");

        printf("%d",ans[i]);
    }
    printf("\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值