Codeforces Round #313 (Div. 2) (ABCDE题解)

来源:互联网 发布:上海行知教育 编辑:程序博客网 时间:2024/05/18 11:28

比赛链接:http://codeforces.com/contest/560



A. Currency System in Geraldion
time limit per test:2 seconds
memory limit per test:256 megabytes

A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is calledunfortunate. Gerald wondered: what is the minimumunfortunate sum?

Input

The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion.

The second line contains n distinct space-separated numbersa1, a2, ..., an (1 ≤ ai ≤ 106) — the values of the banknotes.

Output

Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print  - 1.

Sample test(s)
Input
51 2 3 4 5
Output
-1

题目大意:n种纸币每个面值为ai,问最小的不能组成的面值大小


题目分析:有1输-1,没1输1

#include <cstdio>int main(){int ans = 1, n;scanf("%d", &n);for(int i = 0; i < n; i++){int get;scanf("%d", &get);if(get == 1)ans = -1;}printf("%d\n", ans);}



B. Gerald is into Art
time limit per test:2 seconds
memory limit per test:256 megabytes

Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 × b1 rectangle, the paintings have shape of aa2 × b2 anda3 × b3 rectangles.

Since the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough?

Input

The first line contains two space-separated numbersa1 andb1 — the sides of the board. Next two lines contain numbersa2, b2, a3 andb3 — the sides of the paintings. All numbersai, bi in the input are integers and fit into the range from1 to1000.

Output

If the paintings can be placed on the wall, print "YES" (without the quotes), and if they cannot, print "NO" (without the quotes).

Sample test(s)
Input
3 21 32 1
Output
YES
Input
5 53 33 3
Output
NO
Input
4 22 31 2
Output
YES
Note

That's how we can place the pictures in the first test:

And that's how we can do it in the third one.

题目大意:三个矩形,给出边长,问后两个能不能放到第一个里


题目分析:懒得说,就那么点情况而已


#include <cstdio>#include <algorithm>using namespace std;int a1, a2, a3, b1, b2, b3;bool J(int x, int y){return (x <= a1 && y <= b1 || x <= b1 && y <= a1);}int main(){scanf("%d %d %d %d %d %d", &a1, &b1, &a2, &b2, &a3, &b3);if(J(a2 + a3, max(b2, b3)) || J(b2 + b3, max(a2, a3)) || J(a2 + b3, max(b2, a3)) || J(a3 + b2, max(b3, a2)))printf("YES\n");elseprintf("NO\n");}



C. Gerald's Hexagon
time limit per test:2 seconds
memory limit per test:256 megabytes

Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to. Then he measured the length of its sides, and found that each of them is equal to an integer number of centimeters. There the properties of the hexagon ended and Gerald decided to draw on it.

He painted a few lines, parallel to the sides of the hexagon. The lines split the hexagon into regular triangles with sides of 1 centimeter. Now Gerald wonders how many triangles he has got. But there were so many of them that Gerald lost the track of his counting. Help the boy count the triangles.

Input

The first and the single line of the input contains 6 space-separated integersa1, a2, a3, a4, a5 anda6 (1 ≤ ai ≤ 1000) — the lengths of the sides of the hexagons in centimeters in the clockwise order. It is guaranteed that the hexagon with the indicated properties and the exactly such sides exists.

Output

Print a single integer — the number of triangles with the sides of one 1 centimeter, into which the hexagon is split.

Sample test(s)
Input
1 1 1 1 1 1
Output
6
Input
1 2 1 2 1 2
Output
13
Note

This is what Gerald's hexagon looks like in the first sample:

And that's what it looks like in the second sample:

题目大意:给出六边形6个边长,求里面有多少个边长为1的正三角形


题目分析:这题还有点意思,把它补成大正三角形,大正三角形包含的小三角个数为边长^2,等差数列求和,因为边按顺序给的,通过旋转我们可以发现大边长等于a1+a2+a3,多出来的另外三个正三角形的边长为a1,a3,a5,所以最后答案就是(a1+a2+a3)^2-a1^2-a3^2-a5^2


#include <cstdio>#include <cstring>int const MAX = 3005;int fac[MAX];int main(){for(int i = 1; i <= MAX; i++)fac[i] = i * i;int a[7];for(int i = 1; i <= 6; i++)scanf("%d", &a[i]);printf("%d\n", fac[a[1] + a[2] + a[3]] - fac[a[1]] - fac[a[3]] - fac[a[5]]);}


D. Equivalent Strings
time limit per test:2 seconds
memory limit per test:256 megabytes

Today on a lecture about strings Gerald learned a new definition of string equivalency. Two stringsa andb of equal length are calledequivalent in one of the two cases:

  1. They are equal.
  2. If we split string a into two halves of the same sizea1 anda2, and stringb into two halves of the same sizeb1 andb2, then one of the following is correct:
    1. a1 is equivalent tob1, anda2 is equivalent tob2
    2. a1 is equivalent tob2, anda2 is equivalent tob1

As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.

Gerald has already completed this home task. Now it's your turn!

Input

The first two lines of the input contain two strings given by the teacher. Each of them has the length from1 to200 000 and consists of lowercase English letters. The strings have the same length.

Output

Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise.

Sample test(s)
Input
aabaabaa
Output
YES
Input
aabbabab
Output
NO
Note

In the first sample you should split the first string into strings "aa" and "ba", the second one — into strings "ab" and "aa". "aa" is equivalent to "aa"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a".

In the second sample the first string can be splitted into strings "aa" and "bb", that are equivalent only to themselves. That's why string "aabb" is equivalent only to itself and to string "bbaa".


题目大意:给两个字符串,判断它们是否相等,相等有两种情况,一个是直接相等,一个是切成长度相同的两份以后两子串交叉相等


题目分析:裸的DFS,按照题意搜就行了,如果当前长度为奇数,直接返回false,否则分两种情况搜

这题是水过的,两种情况位置调换T91。。。orz,神人品,不过把string改成char*,怎么都可以过


#include <cstdio>#include <cstring>int const MAX = 250000;char a[MAX], b[MAX];bool DFS(char *p1, char *p2, int len){    if(!strncmp(p1, p2, len))    return true;    if(len % 2)     return false;    int n = len / 2;    if(DFS(p1 ,p2 + n, n) && DFS(p1 + n, p2, n))     return true;    if(DFS(p1, p2, n) && DFS(p1 + n, p2 + n, n))     return true;    return false;}int main(){    scanf("%s %s", a, b);    printf("%s\n", DFS(a, b, strlen(a)) ? "YES" : "NO");}



E. Gerald and Giant Chess
time limit per test:2 seconds
memory limit per test:256 megabytes

Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on anh × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.

Input

The first line of the input contains three integers:h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).

Next n lines contain the description of black cells. Thei-th of these lines contains numbersri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of thei-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.

Output

Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo109 + 7.

Sample test(s)
Input
3 4 22 22 3
Output
2
Input
100 100 315 1616 1599 88
Output
545732279

题目大意:一个矩形,从右上到左下在有n个点不能路过的情况下有几种走法


题目分析:因为h,w比较大,不能开二维,可是n很小,我们从n入手,考虑到每个点不经过坏点有多少种走法,先对点按x优先的从小到大排序

dp[i] = C(xi - 1 + yi - 1, xi - 1)这是从原点到点xi,yi的方案数,因为往右最多走xi-1步,往下最多走yi-1步,因此总共走了xi -1 + yi - 1步,所以走的可能情况直接算组合数,其实xi,yi处是坏点,先假定能在坏点上,然后我们要减去之前遇到的坏点,直接枚举在xi,yi之前的点,那么两个坏点间的走法是C(xi - xj + yi - yj,xi - xj)种,拿dp[i] - Σdp[j],即把到xi,yi经过之前坏点的情况去掉了,注意我们要把终点加到坐标集合中,这样最后到终点时就把所有经过坏点的可能情况都减去了,不妨画个图理解理解,另外这题的要用到组合数取模,要求逆元,板子不好直接导致T20,换了2份板子才过。。。

#include <cstdio>#include <algorithm>using namespace std;#define ll long longint const MOD = 1e9 + 7;ll fac[200005], dp[2005], inv[200005];int h, w, n;struct Point{int x, y;}p[2005];bool cmp(Point a, Point b){if(a.x == b.x)return a.y < b.y;return a.x < b.x;}ll get_inv(ll x){       ll res = 1, y = MOD - 2;    while(y)    {        if(y & 1)            res = (res * x) % MOD;        x = (x * x) % MOD;        y >>= 1;    }    return res;}void pre(){    fac[0] = 1;    inv[0] = 1;    for(int i = 1; i <= 200005; i++)    {        fac[i] = (fac[i - 1] * i) % MOD;        inv[i] = get_inv(fac[i]);    }}ll C(int n, int m){    return fac[n] * inv[m] % MOD * inv[n - m]% MOD;}int main(){    pre();    scanf("%d %d %d", &h, &w, &n);    for(int i = 1; i <= n; i++)    scanf("%d %d", &p[i].x, &p[i].y);    p[++ n].x = h;    p[n].y = w;    sort(p + 1, p + n + 1, cmp);    for(int i = 1; i <= n; i++)    {    dp[i] = C(p[i].x + p[i].y - 2, p[i].x - 1);    for(int j = 1; j < i; j++)    if(p[j].x <= p[i].x && p[j].y <= p[i].y)    dp[i] = (dp[i] % MOD + MOD - dp[j] * C(p[i].x - p[j].x + p[i].y - p[j].y, p[i].x - p[j].x) % MOD) % MOD;    }    printf("%lld\n", dp[n]);}


0 0
原创粉丝点击