Codeforces Round #235 (Div. 2) A~D



A. Vanya and Cards
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.

Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?

You can assume that initially Vanya had infinitely many cards with each integer number from  - x to x.

Input

The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.

Output

Print a single number — the answer to the problem.

Sample test(s)
input
3 2
-1 1 2
output
1
input
2 3
-2 -2
output
2
Note

In the first sample, Vanya needs to find a single card with number -2.

In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    int n,x,a,sum=0;
    cin>>n>>x;
    for(int i=0;i<n;i++) cin>>a,sum+=a;
    sum=abs(sum);
    int ans=0;
    while(sum>0) sum-=x,ans++;
    cout<<ans<<endl;
    return 0;
}

B. Sereja and Contests
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.

Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.

Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed.

Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.

Input

The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in.

Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num2 num1" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.

Output

Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.

Sample test(s)
input
3 2
2 1
2 2
output
0 0
input
9 3
1 2 3
2 8
1 4 5
output
2 3
input
10 0
output
5 9
Note

In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with theDiv1 round.

The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 round

贪心。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int round[5000],x,k;

int main()
{
    cin>>x>>k;
    for(int i=0;i<k;i++)
    {
        int a,b,c;
        cin>>a;
        if(a==1)
        {
            cin>>b>>c;
            round[b]=round[c]=1;
        }
        else if(a==2)
        {
            cin>>b;
            round[b]=1;
        }
    }

    int mmi=0,mmx=0,len=0;
    for(int i=1;i<x;i++)
    {
        if(round[i]==0)
        {
            mmx++;
        }
    }

    for(int i=1;i+1<x;i++)
    {
        if(round[i]==0&&round[i+1]==0)
        {
            mmi++; round[i]=round[i+1]=2;
        }
    }
    for(int i=1;i<x;i++)
    {
        if(round[i]==0)
        {
            mmi++;
        }
    }

    cout<<mmi<<" "<<mmx<<endl;
    return 0;
}

C. Team
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork.

For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that:

  • there wouldn't be a pair of any side-adjacent cards with zeroes in a row;
  • there wouldn't be a group of three consecutive cards containing numbers one.

Today Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.

Input

The first line contains two integers: n (1 ≤ n ≤ 106) — the number of cards containing number 0; m (1 ≤ m ≤ 106) — the number of cards containing number 1.

Output

In a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1.

Sample test(s)
input
1 2
output
101
input
4 8
output
110110110101
input
4 10
output
11011011011011
input
1 5
output
-1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int zero,one;
    cin>>zero>>one;
    int t=min(zero,one);
    if(zero==t&&one==t)
    {
        for(int i=0;i<t;i++) cout<<"10";
        return 0;
    }
    else if(zero>t)
    {
        int d=zero-t;
        if(d>1)
        {
            cout<<-1;
            return 0;
        }
        else
        {
            cout<<"0";
            for(int i=0;i<t;i++) cout<<"10";
        }
        return 0;
    }
    else if(one>t)
    {
        int d=one-t;
        if(d-2>t)
        {
            cout<<-1; return 0;
        }
        else
        {
            int C=d-2;
            for(int i=0;i<t;i++)
            {
                if(i+1<=C) cout<<"1";
                cout<<"10";
            }
            if(d>=2) cout<<"11";
            else if(d==1) cout<<"1";
        }
    }
    return 0;
}



D. Roman and Numbers
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.

Number x is considered close to number n modulo m, if:

  • it can be obtained by rearranging the digits of number n,
  • it doesn't have any leading zeroes,
  • the remainder after dividing number x by m equals 0.

Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.

Input

The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).

Output

In a single line print a single integer — the number of numbers close to number n modulo m.

Sample test(s)
input
104 2
output
3
input
223 4
output
1
input
7067678 8
output
47
Note

In the first sample the required numbers are: 104, 140, 410.

In the second sample the required number is 232.


状压DP。。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

LL dp[1<<18][100],fact[20];
char str[20];
int n,m,cnt[20];

int main()
{
    scanf("%s%d",str,&m);
    n=strlen(str);
    for(int i=0;i<n;i++) cnt[str[i]-'0']++;
    dp[0][0]=1;fact[0]=1;
    for(int i=1;i<20;i++) fact[i]=fact[i-1]*i;
    for(int i=0;i<(1<<n);i++)
    {
        for(int j=0;j<n;j++)
        {
            if(!(i&(1<<j)))
            {
                for(int k=0;k<m;k++)
                {
                    if(!(i==(1<<n)-1&&str[j]=='0')&&!(i==0&&str[j]=='0'))
                        dp[i|(1<<j)][(k*10+str[j]-'0')%m]+=dp[i][k];
                }
            }
        }
    }
    long long int ans=dp[(1<<n)-1][0];
    for(int i=0;i<10;i++)
    {
        ans/=fact[cnt[i]];
    }
    cout<<ans<<endl;
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值