Codeforces Round #235 (Div. 2)

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

题意:就是求一个最小操作数(绝水)

sl:直接模拟。 1 #include <cstdio>

 2 #include <cstring>
 3 #include <algorithm>
 4 typedef long long ll;
 5 using namespace std;
 6 const int MAX =1000+10 ;
 7 int a[MAX];
 8 int main()
 9 {
10     int n,x;
11     int ans;
12     while(scanf("%d %d",&n,&x)==2)
13     {
14         ans=0;
15         for(int i=0;i<n;i++)
16         {
17             scanf("%d",&a[i]);
18             ans+=a[i];
19         }
20         int cnt=0;
21         if(ans==0) printf("0 ");
22         else if(ans>0)
23         {
24             while(ans>=x)
25             {
26                 cnt++;
27                 ans-=x;
28             }
29             if(ans!=0) cnt++;
30             printf("%d ",cnt);
31         }
32         else
33         {
34             while(abs(ans)>=x)
35             {
36                 cnt++;
37                 ans+=x;
38             }
39             if(ans!=0) cnt++;
40             printf("%d ",cnt);
41         }
42     }
43     return 0;

44 } 

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 tox. 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 Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2 num" (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

 题意:每场比赛都有一个特定的标号,每次给出每场比赛的标号,问之前最多和最少参加了多少场div2

sol:还是模拟最多肯定是全部都是div2,最少肯定是都是1,2一起举行,多出来的部分用div2补全

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 typedef long long ll;
 5 using namespace std;
 6 const int MAX = 4000+10;
 7 int vis[MAX];
 8 int main()
 9 {
10     int k,x,t; int num1,num2;
11     while(scanf("%d %d",&x,&k)==2)
12     {
13         memset(vis,0,sizeof(vis));
14         for(int i=0;i<k;i++)
15         {
16             scanf("%d",&t);
17             if(t==2)
18             {
19                 scanf("%d",&num2);
20                 vis[num2]=1;
21             }
22             if(t==1)
23             {
24                 scanf("%d %d",&num1,&num2);
25                 vis[num1]=vis[num2]=1;
26             }
27         }
28         int Min=0,Max=0;
29         for(int i=1;i<x;i++)
30         {
31             if(!vis[i]&&!vis[i+1]&&(i+1)<x)
32             {
33                 Min++;
34                 i++;
35             }
36             else if(i<x&&!vis[i])
37             {
38                 Min++;
39             }
40         }
41         for(int i=1;i<x;i++)
42         {
43             if(!vis[i]) Max++;
44         }
45         printf("%d %d ",Min,Max);
46     }
47     return 0;

48 }

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

题意:就是给出1的个数和0的个数,求出一个满足没有连续的两个0和连续的三个1的序列

sl:一共就三种情况可以算出三个公式吧,很简单,比赛时没注意算了 6种情况

随便来一种情况: |-|-|--|--|--|-

"|"代表0个数设为k  

"-" 代表1总共m个

有: k+(n-1-k)*2+1=m 满足k>=0时有解。其余类似。剩下的就是输出答案了

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 typedef long long ll;
 5 using namespace std;
 6 const int MAX =1e6+10;
 7 int num[MAX];
 8 int main()
 9 {
10     int n,m;
11     while(scanf("%d %d",&n,&m)==2)
12     {
13         int Max=max(n,m);
14         if(n>=m+2) printf("-1 ");
15         else if(n==m+1)
16         {
17             for(int i=0;i<m;i++) printf("01");
18             printf("0");
19             printf(" ");
20         }
21         else if(n==m)
22         {
23             for(int i=0;i<m;i++) printf("01");
24             printf(" ");
25         }
26         else if(2*n-m>=0)
27         {
28             printf("0");
29             for(int i=0;i<2*n-m;i++) printf("10");
30             for(int i=0;i<n-1-2*n+m;i++) printf("110");
31             printf("11");
32             printf(" ");
33         }
34         else if(2*n-m-1>=0)
35         {
36             printf("0");
37             for(int i=0;i<2*n-m-1;i++) printf("10");
38             for(int i=0;i<n-2*n+m;i++) printf("110");
39             printf("1");
40             printf(" ");
41         }
42         else if(2*n-m-2>=0)
43         {
44             printf("0");
45             for(int i=0;i<2*n-m-2;i++) printf("10");
46             for(int i=0;i<n-2*n+m+1;i++) printf("110");
47             printf(" ");
48         }
49         else if(2*n-m+1>=0)
50         {
51             for(int i=0;i<2*n-m+1;i++) printf("10");
52             for(int i=0;i<n-2*n+m-1;i++) printf("110");
53             printf("1");
54             printf(" ");
55         }
56         else if(2*n-m+2>=0)
57         {
58             for(int i=0;i<2*n-m+2;i++) printf("10");
59             for(int i=0;i<n-2*n+m-2;i++) printf("110");
60             printf("11");
61             printf(" ");
62         }
63         else if(2*n-m>=0)
64         {
65             for(int i=0;i<2*n-m;i++) printf("10");
66             for(int i=0;i<n-2*n+m;i++) printf("110");
67             printf(" ");
68         }
69     }
70     return 0;

71 } 

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

 题意:给出一个数字n和m求出与数字n相近的数,这个数满足:他是n的每位数字的重新排列,用它%m为0,没有前导0。求满足条件的总数。

sol: dp题目。--(当时不会么,状压学的太烂了) ,枚举每一位的状态,(从后枚举)

方程:dp[i|1<<j][(k*10+a[j])%m]+=dp[i][k];  dp【i】[j] 表示每位状态为i余数为j时的总数。

具体参考代码。 1 #include<cstdio>

 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 long long dp[1<<18][100],d,f[20];
 7 int main()
 8 {
 9     int m,l,n,i,j,k,c[10];
10     char a[20];
11     memset(c,0,sizeof(c));
12     for(f[0]=1,i=1;i<20;i++)
13     f[i]=f[i-1]*i; //计算阶乘
14     scanf("%s%d",a,&m);
15 
16     l=strlen(a),n=1<<l;
17     for(i=0;i<l;i++)
18     c[a[i]-='0']++; //保存每个数字的个数
19 
20     for(d=1,i=0;i<10;i++)
21     d*=f[c[i]];  //重复的个数
22 
23     dp[0][0]=1;
24 
25     for(i=0;i<n;i++)
26     for(j=0;j<l;j++)
27     if(!(i&1<<j))  //此处数字已经去过了不用再去取了从后往前取
28     for(k=0;k<m;k++)
29     if(!(i==0&&a[j]==0))  //当i!=0时 最高位肯定不是0随便取
30     dp[i|1<<j][(k*10+a[j])%m]+=dp[i][k];
31 
32     printf("%I64d ",dp[n-1][0]/d);
33     return 0;

34 } 


 PS:yy了一下大牛,貌似可以将数列,分成两半然后暴力求解时间复杂度为O(2^9);


原文地址:https://www.cnblogs.com/acvc/p/3593467.html