题意 给你两个二进制数m,n 求他们的最大公约数 用二进制表示 0<m,n<2^1000

先把二进制转换为十进制 求出最大公约数 再把结果转换为二进制 数比较大要用到大数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
import java.math.*;

public class wl6_9 {

static BigInteger two = BigInteger.valueOf(2), one = BigInteger.ONE,
zero = BigInteger.ZERO;

static BigInteger gcd(BigInteger a, BigInteger b) {
while (!(a.mod(b).equals(zero))) {
BigInteger t = a.mod(b);
a = b;
b = t;
}
return b;
}

static void bprint(BigInteger x) {
if (x.equals(zero))
return;
bprint(x.divide(two));
if (x.mod(two).equals(one))
System.out.print(1);
else
System.out.print(0);
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
String a, b;
for (int t = 1; t <= T; t++) {
a = in.next();
b = in.next();
int la = a.length(), lb = b.length();

BigInteger m = zero, n = zero;
for (int i = 0; i < la; ++i) {
m = m.multiply(two);
if (a.charAt(i) == '1')
m = m.add(one);
}

for (int i = 0; i < lb; ++i) {
n = n.multiply(two);
if (b.charAt(i) == '1')
n = n.add(one);
}
System.out.printf("Case #%d: ", t);
bprint(gcd(m, n));
System.out.println();
}
in.close();
}
}

Divided Land

Problem Description

It’s time to fight the local despots and redistribute the land. There is a rectangular piece of land granted from the government, whose length and width are both in binary form. As the mayor, you must segment the land into multiple squares of equal size for the villagers. What are required is there must be no any waste and each single segmented square land has as large area as possible. The width of the segmented square land is also binary.
Input

The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.
Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 21000)
Output

For each test case, print a line “Case /#t: ”(without quotes, t means the index of the test case) at the beginning. Then one number means the largest width of land that can be divided from input data. And it will be show in binary. Do not have any useless number or space.
Sample Input

3 10 100 100 110 10010 1100
Sample Output

Case /#1: 10 Case /#2: 10 Case /#3: 110
Source

2014 ACM/ICPC Asia Regional Shanghai Online

题意 判断一个树状天平是否平衡 每个测试样例每行4个数 wl,dl,wr,dr 当wl/*dl=wr/*dr时 视为这个天平平衡 当wl或wr等于0是 下一行将是一个子天平 如果子天平平衡 wl为子天平的wl+wr 否则整个天平不平衡

容易看出 输入是递归的 所以我们可以直接递归 边输入边判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<cstdio>
using namespace std;

bool solve(int &w)
{
int wl, dl, wr, dr;
bool mobl = true, mobr = true;
scanf("%d %d %d %d", &wl, &dl, &wr, &dr);
if(!wl) mobl = solve(wl);
if(!wr) mobr = solve(wr);
w = wl + wr;
return mobl && mobr && (wl * dl == wr * dr);
}

int main()
{
int cas, w;
scanf("%d", &cas);
while(cas--)
{
printf(solve(w) ? "YES\n" : "NO\n");
if(cas) printf("\n");
}
return 0;
}


Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.

\epsfbox{p839a.eps}

The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. From the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is WDl = WDrwhere Dl is the left distance, Dr is the right distance, Wl is the left weight and Wr is the right weight.

In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure. In this case it is not so straightforward to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium or not.

\epsfbox{p839b.eps}

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input is composed of several lines, each containing 4 integers separated by a single space. The 4 integers represent the distances of each object to the fulcrum and their weights, in the format: Wl Dl Wr Dr

If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the the sub-mobile. In this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding the weight of the wires and strings. If both Wl and Wrare zero then the following lines define two sub-mobiles: first the left then the right one.

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Write YES' if the mobile is in equilibrium, write NO’ otherwise.

1 0 2 0 4 0 3 0 1 1 1 1 1 2 4 4 2 1 6 3 2

YES


Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.

\epsfbox{p839a.eps}

The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. From the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is WDl = WDrwhere Dl is the left distance, Dr is the right distance, Wl is the left weight and Wr is the right weight.

In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure. In this case it is not so straightforward to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium or not.

\epsfbox{p839b.eps}

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input is composed of several lines, each containing 4 integers separated by a single space. The 4 integers represent the distances of each object to the fulcrum and their weights, in the format: Wl Dl Wr Dr

If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the the sub-mobile. In this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding the weight of the wires and strings. If both Wl and Wrare zero then the following lines define two sub-mobiles: first the left then the right one.

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Write YES' if the mobile is in equilibrium, write NO’ otherwise.

1 0 2 0 4 0 3 0 1 1 1 1 1 2 4 4 2 1 6 3 2

YES

题意 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每堆有多少片叶子

和上一题有点像 都是递归输入的 一个节点(设水平位置为p) 则它的左右儿子节点的水平位置分别为 p-1 p+1 也是可以边输入边处理的 输入完也就得到答案了 注意每个样例后面都有一个空行 包括最后一个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<cstdio>
#include<cstring>
using namespace std;
int le, ri;
const int M = 205;
int a[M];

void build(int p)
{
int t;
scanf("%d", &t);
if(t == -1) return;
else
{
a[p] += t;
build(p - 1);
build(p + 1);
le = le < p ? le : p;
ri = ri > p ? ri : p;
}
}

int main()
{
int cas = 0;
le = M, ri = 0;
build(M / 2);

while(le <= ri)
{
printf("Case %d:\n", ++cas);
for(int i = le; i < ri; ++i)
printf("%d ", a[i]);
printf("%d\n\n", a[ri]);

memset(a, 0, sizeof(a));
le = M, ri = 0;
build(M / 2);
}
return 0;
}


Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?

We assume each node in a binary tree “drops” a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there’s no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the “leaves” drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value -1 is supplied. Thus the tree shown above is specified as 5 7 -1 6 -1 -1 3 -1 -1 . Each actual tree node contains a positive, non-zero value. The last test case is followed by a single -1 (which would otherwise represent an empty tree).

For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of “leaves” in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the examples below.

5 7 -1 6 -1 -1 3 -1 -1 8 2 9 -1 -1 6 5 -1 -1 12 -1 -1 3 7 -1 -1 -1 -1

Case 1: 7 11 3 Case 2: 9 7 21 15

题意 给你一个树的中序遍历和后序遍历 某个节点的权值为从根节点到该节点所经过节点的和 求权值最小的叶节点的值 如果存在多个 输出值最小的那个

把树建好就好说了 递归递归dfs msun保存最小叶节点权值 ans保存答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
const int maxn = 10005, INF = 0x3f3f3f3f;
int in[maxn], post[maxn], msum, ans;

struct node
{
int val;
node *left, *right;
node(): left(NULL), right(NULL) {}
};

node* build(int le, int ri, int pos)
{
node *cur = new node;
cur->val = post[pos];
if(le == ri) return cur;
int i = le;
while(in[i] != post[pos]) ++i;
if(i == le) cur->left = NULL;
else cur->left = build(le, i - 1, pos - 1 - (ri - i));
if(i == ri) cur->right = NULL;
else cur->right = build(i + 1, ri, pos - 1);
return cur;
}

void dfs(node *cur, int sum)
{
int t = cur->val;
if(!(cur->left || cur->right))
{
if(sum + t < msum || (sum + t == msum && t < ans))
msum = sum + t, ans = t;
}
if(cur->left != NULL) dfs(cur->left, sum + t);
if(cur->right != NULL) dfs(cur->right, sum + t);

}

int main()
{
char s[maxn*10];
while(gets(s) != NULL)
{
int t = 0, l = 0;
for(int i = 0; s[i] != '\0'; ++i)
if(isdigit(s[i])) t = t * 10 + s[i] - '0';
else in[++l] = t, t = 0;
in[++l] = t;

gets(s);
l = t = 0;
for(int i = 0; s[i] != '\0'; ++i)
if(isdigit(s[i])) t = t * 10 + s[i] - '0';
else post[++l] = t, t = 0;
post[++l] = t;

node *root = build(1, l, l);
ans = msum = INF;
dfs(root, 0);
printf("%d\n", ans);
}
return 0;
}


You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

3 2 1 4 5 7 6 3 1 2 5 6 7 4 7 8 11 3 5 16 12 18 8 3 11 7 16 18 12 5 255 255

1 3 255


You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.

For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.

3 2 1 4 5 7 6 3 1 2 5 6 7 4 7 8 11 3 5 16 12 18 8 3 11 7 16 18 12 5 255 255

1 3 255

题意 建树并层次遍历输出 (data,pos) pos表示改节点位置 L代表左儿子 R代表右儿子

建树很简单 开始在根节点 遇到L往左走遇到R往右走 节点不存在就新建 走完了就保存改节点的值 输出直接bfs就行了了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
const int maxn = 300;

char s[maxn][maxn];
int t, n, lans;
bool comp;

struct Node
{
int data;
Node* left, *right;
bool have;
Node(): left(NULL), right(NULL), have(false) {};
};

Node *ans[maxn];

bool bfs(Node* root)
{
int fro = 0, bac = 0;
ans[fro] = root;
while(fro <= bac)
{
if(!ans[fro]->have) return false;
if(ans[fro]->left != NULL) ans[++bac] = ans[fro]->left;
if(ans[fro]->right != NULL) ans[++bac] = ans[fro]->right;
++fro;
}
lans = bac;
return true;
}

int main()
{
while(~scanf("%s", s[0]))
{
n = 0;
Node root, *cur = &root;
comp = true;
do
{
t = 0;
int l = strlen(s[n]);
for(int i = 1; i < l; ++i)
{
if(isdigit(s[n][i]))
{
t = t * 10 + s[n][i] - '0';
}
else if(s[n][i] == 'L')
{
if(cur->left == NULL)
cur->left = new Node;
cur = cur->left;
}
else if(s[n][i] == 'R')
{
if(cur->right == NULL)
cur->right = new Node;
cur = cur->right;
}
}
if(cur->have) comp = false;
else
{
cur->data = t;
cur->have = true;
}
cur = &root;
}
while(scanf("%s", s[++n]), s[n][1] != ')');

if(comp) comp = bfs(&root);
if(comp)
{
for(int i = 0; i < lans; ++i)
printf("%d ", ans[i]->data);
printf("%d\n", ans[lans]->data);
}
else printf("not complete\n");
}
return 0;
}


Background

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.

This problem involves building and traversing binary trees.

The Problem

Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.

For example, a level order traversal of the tree

picture28

is: 5, 4, 8, 11, 13, 4, 7, 2, 1.

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L‘s and R‘s where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.

The Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses.

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.

The Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete’’ should be printed.

Sample Input

(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) () (3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1 not complete

题意 有一个键盘坏了 会在你不知道的情况下按下home或者end 给你这个键盘的实际输入 要求输出显示器上的实际显示

输入最大5MB 所以直接数组检索肯定会超时的 用数组模拟链表 就可以很快了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100005;
char s[N];
int next[N];
int main()
{
int last,cur;
while(~scanf("%s",s+1))
{
next[0]=last=cur=0;
int length=strlen(s+1);
for(int i=1;i<=length;++i)
{
if(s[i]=='[') cur=0;
else if(s[i]==']') cur=last;
else
{
next[i]=next[cur];
next[cur]=i;
if(cur==last) last=i;
cur=i;
}
}
for(int i=next[0];i;i=next[i])
printf("%c",s[i]);
printf("\n");
}
return 0;
}

Broken Keyboard

You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed (internally).

You’re not aware of this issue, since you’re focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).

In Chinese, we can call it Beiju. Your task is to find the Beiju text.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[‘ and ‘]’. ‘[‘ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each case, print the Beiju text on the screen.

Sample Input

This_is_a_[Beiju]_text [[]][][]Happy_Birthday_to_Tsinghua_University

Output for the Sample Input

BeijuThis_is_a__text Happy_Birthday_to_Tsinghua_University

题意 计算给定矩阵链乘表达式需要计算的次数 当前一个矩阵的列数等于后一个矩阵的行数时 他们才可以相乘 不合法输出error

输入是严格合法的 即使只有两个相乘也会用括号括起来 而且括号里最多有两个 那么就很简单了 遇到字母直接入栈 遇到反括号计算后入栈 然后就得到结果了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
const int N = 1000;
int st[N], row[N], col[N], r[N], c[N];

int main()
{
int n, ans, top;
scanf("%d", &n);
char na[3], s[N];
for(int i = 1; i <= n; ++i)
{
scanf("%s", na);
int j = na[0] - 'A';
scanf("%d%d", &row[j], &col[j]);
}

while(~scanf("%s", &s))
{
int i;
for(i = 0 ; i < 26; ++i)
c[i] = col[i], r[i] = row[i];
ans = top = 0;

for(i = 0; s[i] != '\0'; ++i)
{
if(isalpha(s[i]))
{
int j = s[i] - 'A';
st[++top] = j;
}

else if(s[i] == ')')
{
if(r[st[top]] != c[st[top - 1]]) break;
else
{
--top;
c[st[top]] = c[st[top + 1]];
ans += (r[st[top]] * c[st[top]] * r[st[top + 1]]);
}
}
}
if(s[i] == '\0') printf("%d\n", ans);
else printf("error\n");
}
return 0;
}


Suppose you have to evaluate an expression like A/*B/*C/*D/*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of elementary multiplications needed strongly depends on the evaluation order you choose.

For example, let A be a 50/*10 matrix, B a 10/*20 matrix and C a 20/*5 matrix. There are two different strategies to compute A/*B/*C, namely (A/*B)/C and A/(B/*C).

The first one takes 15000 elementary multiplications, but the second one only 3500.

Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy.

Input Specification

Input consists of two parts: a list of matrices and a list of expressions.

The first line of the input file contains one integer n ( tex2html_wrap_inline28 ), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix.

The second part of the input file strictly adheres to the following syntax (given in EBNF):

SecondPart = Line { Line } Line = Expression Expression = Matrix | “(“ Expression Expression “)” Matrix = “A” | “B” | “C” | … | “X” | “Y” | “Z”

Output Specification

For each expression found in the second part of the input file, print one line containing the word “error” if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses.

Sample Input

9 A 50 10 B 10 20 C 20 5 D 30 35 E 35 15 F 15 5 G 5 10 H 10 20 I 20 25 A B C (AA) (AB) (AC) (A(BC)) ((AB)C) (((((DE)F)G)H)I) (D(E(F(G(HI))))) ((D(EF))((GH)I))

Sample Output

0 0 0 error 10000

题意 n辆火车按顺序依次进站 判断给定的出战顺序是否可能

用数组模拟模拟栈代表车站 车依次进站 每当栈顶火车序号与当前要出站的b[cur] 相等时 就让栈顶元素出栈 即top–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 2000;
int b[N], c[N];
int main()
{
int l, cur, top;
while(scanf("%d", &l), l)
{
while(scanf("%d", &b[1]), b[1])
{
for(int i = 2; i <= l; ++i)
scanf("%d", &b[i]);
cur = 1, top = 0;
for(int i = 1; i <= l; ++i)
{
if(i == b[cur]) cur++;
else c[++top] = i;
while(top && b[cur] == c[top])
{
++cur;
--top;
}
}
printf("%s\n", top ? "No" : "Yes");
}
printf("\n");
}
return 0;
}


There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

\begin{picture}(6774,3429)(0,-10)\put(1789.500,1357.500){\arc{3645.278}{4.7247}......tFigFont{14}{16.8}{\rmdefault}{\mddefault}{\updefault}Station}}}}}\end{picture}

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has$N \leŸ 1000$ coaches numbered in increasing order $1, 2, \dots, N$. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be $a_1. a_2, \dots, a_N$. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

The input file consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of $1, 2, \dots, N$ The last line of the block contains just 0.

The last block consists of just one line containing 0.

The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains No . In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last ``null’’ block of the input file.

5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0

Yes No Yes

题意 输入a1,a2,a3,a4,a5 求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立 a,x取值在-50到50之间

直接暴力的话肯定会超时的 100的五次方 10e了都 然后可以考虑将等式变一下形 把a1/*x1^3+a2/*x2^3移到右边 也就是-(a1/*x1^3+a2^x2^3)=a3/*x3^3+a4/*x4^3+a5/*x5^3

考虑到a1/*x1^3+a2^x2^3的最大值50/*50^3+50/*50^3=12500000 这个数并不大 可以开这么大的数组把每个结果出现的次数存下来 又因为结果最小可能是负的12500000 负数不能做数组的下标 加上个12500000/*2就行了 这样分别枚举左右两边 把左边出现过的结果都存在一个数组里面 再枚举右边 没出现一次结果 答案就加上前面这个结果出现的次数 枚举完就出现答案了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<cstdio>
#include<cstring>
using namespace std;
const int maxs = 50 * 50 * 50 * 50 * 4 + 10;
unsigned short cnt[maxs];
int main()
{
int a1, a2, a3, a4, a5, sum,ans=0;
scanf ("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
for (int x1 = -50; x1 <= 50; ++x1)
{
if (x1 == 0) ++x1;
for (int x2 = -50; x2 <= 50; ++x2)
{
if (x2 == 0) ++x2;
sum = (a1 * x1 * x1 * x1 + a2 * x2 * x2 * x2) * (-1);
if (sum < 0) ++cnt[sum + maxs];
else ++cnt[sum];
}
}

for (int x3 = -50; x3 <= 50; ++x3)
{
if (x3 == 0) ++x3;
for (int x4 = -50; x4 <= 50; ++x4)
{
if (x4 == 0) ++x4;
for (int x5 = -50; x5 <= 50; ++x5)
{
if (x5 == 0) ++x5;
sum = (a3 * x3 * x3 * x3 + a4 * x4 * x4 * x4 + a5 * x5 * x5 * x5) ;
if (sum < 0) sum += maxs;
ans += cnt[sum];
}
}
}
printf ("%d\n", ans);
return 0;
}

Eqs

Description
Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.

Input

The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

Output

The output will contain on the first line the number of the solutions for the given equation.

Sample Input

37 29 41 43 47

Sample Output

654

还有用hashmap做的 空间优化了不少

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<cstdio>
#include<cstring>
#include<hash_map>
using namespace std;
int first[50*50*50+10];
int ecnt,w[10005],v[10005],nex[10005];

void add(int x)
{
int t=(x+50*50*50*100)/200,flag=1;
for(int e=first[t];(~e)&&flag;e=nex[e])
{
if(v[e]==x)
{
flag=0,w[e]++;
}
}
if(flag)
{
w[ecnt]=1;
v[ecnt]=x;
nex[ecnt]=first[t];
first[t]=ecnt++;
}
}
int getcnt(int x)
{
if(x>50*50*50*50*2||x<-50*50*50*50*2)return 0;
int t=(x+50*50*50*100)/200;
for(int e=first[t];(~e);e=nex[e])
{
if(v[e]==x)return w[e];
}
return 0;
}
int main()
{
int a1, a2, a3, a4, a5, sum,ans=0;
scanf ("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
memset(first,-1,sizeof first);
ecnt=0;
for (int x1 = -50; x1 <= 50; ++x1)
{
if (x1 == 0) ++x1;
for (int x2 = -50; x2 <= 50; ++x2)
{
if (x2 == 0) ++x2;
sum = (a1 * x1 * x1 * x1 + a2 * x2 * x2 * x2) * (-1);
add(sum);
}
}
for (int x3 = -50; x3 <= 50; ++x3)
{
if (x3 == 0) ++x3;
for (int x4 = -50; x4 <= 50; ++x4)
{
if (x4 == 0) ++x4;
for (int x5 = -50; x5 <= 50; ++x5)
{
if (x5 == 0) ++x5;
sum = (a3 * x3 * x3 * x3 + a4 * x4 * x4 * x4 + a5 * x5 * x5 * x5) ;
ans +=getcnt(sum);
}
}
}
printf ("%d\n", ans);
return 0;
}

题意 有c个小孩 n个大人万圣节搞活动 当小孩进入第i个大人家里时 这个大人就会给小孩a[i]个糖果 求小孩去哪几个大人家可以保证得到的糖果总数是小孩数c的整数倍 多种方案满足输出任意一种

用s[i]表示前i个打人给糖果数的总和 令s[0]=0 那么s[i]共有n+1种不同值 而s[i]%c最多有c种不同值 题目说了c<=n 所以s[i]%c肯定会有重复值了

这就是抽屉原理了 n个抽屉放大于n个苹果 至少有一个抽屉有大于等于2个苹果

就把s[i]%c的取值个数(c)看作是抽屉 然后s[i]的取值个数看作是苹果 就有上面的结论了

然后当s[a]%c==s[b]%c的时候 可以知道(s[b]-s[a])%c是等于0的 也就是说得到了一个方案 选从a+1到第b个打人就可以平均分糖果了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 100010;
int s[N], vis[N];

int main()
{
int n, c, i, j;
while (~scanf ("%d%d", &c, &n), c)
{
memset (vis, 0, sizeof (vis));
vis[0] = 1;
for (i = 1; i <= n; ++i)
{
scanf ("%d", &s[i]);
s[i] = (s[i] + s[i - 1]) % c;
}

for (i = 1; i <= n; ++i)
{
if (vis[s[i]]) break;
else
vis[s[i]] = i+1;
}

for (j = vis[s[i]]; j <= i; ++j)
{
if (j != i) printf ("%d ", j);
else printf ("%d\n", j);
}
}
return 0;
}

Halloween treats

Description
Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too late. To avoid conflicts, the children have decided they will put all sweets together and then divide them evenly among themselves. From last year’s experience of Halloween they know how many sweets they get from each neighbour. Since they care more about justice than about the number of sweets they get, they want to select a subset of the neighbours to visit, so that in sharing every child receives the same number of sweets. They will not be satisfied if they have any sweets left which cannot be divided.

Your job is to help the children and present a solution.

Input

The input contains several test cases.
The first line of each test case contains two integers c and n (1 ≤ c ≤ n ≤ 100000), the number of children and the number of neighbours, respectively. The next line contains nspace separated integers *a1 , … , *a**n** (1 ≤ ai ≤ 100000), where ai** represents the number of sweets the children get if they visit neighbour i.

The last test case is followed by two zeros.

Output

For each test case output one line with the indices of the neighbours the children should select (here, index i corresponds to neighbour i who gives a total number of ai sweets). If there is no solution where each child gets at least one sweet print “no sweets” instead. Note that if there are several solutions where each child gets at least one sweet, you may print any of them.

Sample Input

4 5 1 2 3 7 5 3 6 7 11 2 5 13 17 0 0

Sample Output

3 5 2 3 4

0%