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 =+ ;
 7 int a[MAX];
 8 int main()
 9 {
     int n,x;
     int ans;
     while(scanf("%d %d",&n,&x)==)
     {
         ans=;
         for(int i=;i<n;i++)
         {
             scanf("%d",&a[i]);
             ans+=a[i];
         }
         int cnt=;
         if(ans==) printf("0\n");
         else if(ans>)
         {
             while(ans>=x)
             {
                 cnt++;
                 ans-=x;
             }
             if(ans!=) cnt++;
             printf("%d\n",cnt);
         }
         else
         {
             while(abs(ans)>=x)
             {
                 cnt++;
                 ans+=x;
             }
             if(ans!=) cnt++;
             printf("%d\n",cnt);
         }
     }
     return ;

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 numnum1" (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 = +;
 7 int vis[MAX];
 8 int main()
 9 {
     int k,x,t; int num1,num2;
     while(scanf("%d %d",&x,&k)==)
     {
         memset(vis,,sizeof(vis));
         for(int i=;i<k;i++)
         {
             scanf("%d",&t);
             if(t==)
             {
                 scanf("%d",&num2);
                 vis[num2]=;
             }
             if(t==)
             {
                 scanf("%d %d",&num1,&num2);
                 vis[num1]=vis[num2]=;
             }
         }
         int Min=,Max=;
         for(int i=;i<x;i++)
         {
             if(!vis[i]&&!vis[i+]&&(i+)<x)
             {
                 Min++;
                 i++;
             }
             else if(i<x&&!vis[i])
             {
                 Min++;
             }
         }
         for(int i=;i<x;i++)
         {
             if(!vis[i]) Max++;
         }
         printf("%d %d\n",Min,Max);
     }
     return ;

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+;
 7 int num[MAX];
 8 int main()
 9 {
     int n,m;
     while(scanf("%d %d",&n,&m)==)
     {
         int Max=max(n,m);
         if(n>=m+) printf("-1\n");
         else if(n==m+)
         {
             for(int i=;i<m;i++) printf("");
             printf("");
             printf("\n");
         }
         else if(n==m)
         {
             for(int i=;i<m;i++) printf("");
             printf("\n");
         }
         else if(*n-m>=)
         {
             printf("");
             for(int i=;i<*n-m;i++) printf("");
             for(int i=;i<n--*n+m;i++) printf("");
             printf("");
             printf("\n");
         }
         else if(*n-m->=)
         {
             printf("");
             for(int i=;i<*n-m-;i++) printf("");
             for(int i=;i<n-*n+m;i++) printf("");
             printf("");
             printf("\n");
         }
         else if(*n-m->=)
         {
             printf("");
             for(int i=;i<*n-m-;i++) printf("");
             for(int i=;i<n-*n+m+;i++) printf("");
             printf("\n");
         }
         else if(*n-m+>=)
         {
             for(int i=;i<*n-m+;i++) printf("");
             for(int i=;i<n-*n+m-;i++) printf("");
             printf("");
             printf("\n");
         }
         else if(*n-m+>=)
         {
             for(int i=;i<*n-m+;i++) printf("");
             for(int i=;i<n-*n+m-;i++) printf("");
             printf("");
             printf("\n");
         }
         else if(*n-m>=)
         {
             for(int i=;i<*n-m;i++) printf("");
             for(int i=;i<n-*n+m;i++) printf("");
             printf("\n");
         }
     }
     return ;

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[<<][],d,f[];
 7 int main()
 8 {
 9     int m,l,n,i,j,k,c[];
     char a[];
     memset(c,,sizeof(c));
     for(f[]=,i=;i<;i++)
     f[i]=f[i-]*i; //计算阶乘
     scanf("%s%d",a,&m);
 
     l=strlen(a),n=<<l;
     for(i=;i<l;i++)
     c[a[i]-='']++; //保存每个数字的个数
 
     for(d=,i=;i<;i++)
     d*=f[c[i]];  //重复的个数
 
     dp[][]=;
 
     for(i=;i<n;i++)
     for(j=;j<l;j++)
     if(!(i&<<j))  //此处数字已经去过了不用再去取了从后往前取
     for(k=;k<m;k++)
     if(!(i==&&a[j]==))  //当i!=0时 最高位肯定不是0随便取
     dp[i|<<j][(k*+a[j])%m]+=dp[i][k];
 
     printf("%I64d\n",dp[n-][]/d);
     return ;

34 }

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

Codeforces Round #235 (Div. 2)的更多相关文章

  1. Codeforces Round #235 (Div. 2) D. Roman and Numbers(如压力dp)

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

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp

    题目链接: http://codeforces.com/problemset/problem/401/D D. Roman and Numbers time limit per test4 secon ...

  3. Codeforces Round #235 (Div. 2)C、Team

    #include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; cin & ...

  4. Codeforces Round #235 (Div. 2) B. Sereja and Contests

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  5. Codeforces Round #235 (Div. 2) A. Vanya and Cards

    #include <iostream> using namespace std; int main(){ int n,x; cin >> n >> x; ; ; i ...

  6. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

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

  7. Codeforces Round #235 (Div. 2) C. Team

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

  8. Codeforces Round #235 (Div. 2) D (dp)

    以为是组合,后来看着像数位dp,又不知道怎么让它不重复用..然后就没思路 了. 其实状压就可以了 状压是可以确定一个数的使用顺序的 利用01也可以确定所有的数的使用以及不重复 dp[i+1<&l ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. poj Find a multiple【鸽巢原理】

    参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...

  2. less新手入门(一) 变量、extend扩展

    前景提要 个人在学习less时候的学习笔记及个人总结,主要是结合less中文网来学习的,但是说是中文网并不是中文呀,看起来很耽误时间,为了避免以后再次看的时候还要翻译思考,特意做此总结,方便以后查阅. ...

  3. 【[转】MySql模糊查询

    转自:http://chenpeng.info/html/2020 MySQL提供标准的SQL模式匹配,以及一种基于象Unix实用程序如vi.grep和sed的扩展正则表达式模式匹配的格式. 一.SQ ...

  4. C# 调用非托管函数

    C#通过DllImport可以直接调用Windows中的一些功能.C++中已经编写好的一些方法: DllImport所在的名字空间:System.Runtime.InteropServices; Dl ...

  5. redis之有序集合类型(Zset)——排行榜的实现

    当数据库对排序支持的不是很好,可以利用redis有序集合排序 原文链接:http://blog.csdn.net/loophome/article/details/50373202

  6. 这样的设计是否有违背MVC设计原则??

    MVC 皆知为 Model-View-Controller 请求模型-〉Client发现请求-〉Controller接收+处理-〉返回Model给前端-〉前端接收处理模型Result 但是最近发现一个 ...

  7. ubantu MongoDB安装

    转 https://blog.csdn.net/flyfish111222/article/details/51886787 本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的 ...

  8. JDBC更新10W级以上数据性能优化

    随笔缘由: 系统完成到一定程度,少不了要往数据库中添加大量数据进行性能测试. 我用程序做数据10W条,使用jdbc批更新的API,发现每次只能插入2W多条记录. 一番小小研究,觉得总结一下可能有些意义 ...

  9. 8月中旬之后的学习计划 --- react

    这段时间快活了,放纵了,玩hi了,接下来该好好的学习新知识了. 鉴于目前业界比较火的前端js框架有react.vue,决定先从react开始学习.之前有简单的接触过它的一些基本的语法知识,这次准备全面 ...

  10. 使用Unittest做单元测试,addTest()单个case的时候却执行全部的case

    参考: http://tieba.baidu.com/p/6008699660 首先造成这个结果的原因是pycharm配置问题 问题验证: 测试代码: import unittest class Te ...