ACM竞赛试题集锦
acm数学竞赛试题
acm数学竞赛试题
ACM数学竞赛试题通常涉及各种数学领域,包括但不限于代数、几何、概率统计和组合数学等。
以下是一些经典的ACM数学竞赛试题:
1. 平面上n个点的k距离和最小值问题:给定平面上n个点,对于每个点,计算它到其他所有点的距离,然后求出这些距离中的k个最小值。
问题是:如何有效地计算这k个最小值?
2.最长公共子序列问题:给定两个序列,找出它们的最长公共子序列。
例如,对于序列
A = [1, 2, 3, 4] 和
B = [2, 3, 4, 5],最长公共子序列是[2, 3, 4]。
3. 凸包问题:给定平面上的一组点,找到一个最小的凸多边形,使得这个多边形能够包含这组点中的所有点。
4. 最短路问题:给定一个有向图,其中每条边都有一个非负的权重,找出图中任意两点之间的最短路径。
5. 子集和问题:给定一个正整数数组和一个目标值,判断数组中是否存在和为目标值的两个非空子集。
例如,给定数组[1, 2, 3, 4] 和目标值7,判断是否存在两个子集,它们的和分别为7。
以上只是ACM数学竞赛试题的一部分,实际上还有更多涉及数学各个领域的题目。
要提高解决这类问题的能力,需要不断练习和研究。
acm初赛试题及答案
acm初赛试题及答案# acm初赛试题及答案1. 问题描述给定一个整数n,求出n以内所有正整数的和。
2. 输入格式输入包含一个整数n(1≤n≤10000)。
3. 输出格式输出一个整数,表示n以内所有正整数的和。
4. 样例输入```100```5. 样例输出```5050```6. 问题分析本题要求计算从1到n的所有正整数的和。
这是一个简单的数学问题,可以通过公式求解。
7. 解题思路使用求和公式:\( \sum_{i=1}^{n} i = \frac{n(n+1)}{2} \)。
8. 算法实现编写一个函数,输入n,输出计算结果。
9. 代码示例```pythondef sum_of_numbers(n):return n * (n + 1) // 2n = int(input())print(sum_of_numbers(n))```10. 注意事项输入的n可能会非常大,需要考虑整数溢出的问题。
11. 测试用例- 输入:1输出:1- 输入:10输出:55- 输入:10000输出:5000500012. 评分标准- 正确性:算法正确,输出结果符合预期。
- 效率:算法执行效率高,能在规定时间内完成计算。
- 代码风格:代码可读性好,注释清晰。
13. 常见错误- 未考虑整数溢出。
- 未正确使用求和公式。
- 代码中存在语法错误。
14. 扩展思考- 如果要求计算从m到n的所有正整数的和,应该如何修改算法? - 如何优化算法以处理更大的输入值?15. 参考答案- 对于扩展思考的第一个问题,可以将求和公式修改为:\( \sum_{i=m}^{n} i = \frac{n(n+1)}{2} - \frac{(m-1)m}{2} \)。
- 对于扩展思考的第二个问题,可以考虑使用更高效的数据结构或者算法来减少计算量。
以上是ACM初赛试题及答案的完整内容。
计算机acm试题及答案
计算机acm试题及答案一、选择题1. 在计算机科学中,ACM代表什么?A. 人工智能与机器学习B. 计算机辅助制造C. 计算机辅助设计D. 国际计算机学会答案:D2. 下列哪个不是计算机程序设计语言?A. PythonB. JavaC. C++D. HTML答案:D3. 在计算机系统中,CPU代表什么?A. 中央处理单元B. 计算机辅助设计C. 计算机辅助制造D. 计算机辅助教学答案:A二、填空题1. 计算机的内存分为__________和__________。
答案:RAM;ROM2. 在编程中,__________是一种用于存储和操作数据的数据结构。
答案:数组3. 计算机病毒是一种__________,它能够自我复制并传播到其他计算机系统。
答案:恶意软件三、简答题1. 请简述计算机操作系统的主要功能。
答案:计算机操作系统的主要功能包括管理计算机硬件资源,提供用户界面,运行应用程序,以及控制其他系统软件和应用软件的运行。
2. 什么是云计算,它与传统的本地计算有何不同?答案:云计算是一种通过互联网提供计算资源(如服务器、存储、数据库、网络、软件等)的服务模式。
与传统的本地计算相比,云计算允许用户按需获取资源,无需购买和维护物理硬件,具有更高的灵活性和可扩展性。
四、编程题1. 编写一个程序,计算并输出从1到100(包括1和100)之间所有偶数的和。
答案:```pythonsum = 0for i in range(1, 101):if i % 2 == 0:sum += iprint(sum)```2. 给定一个字符串,编写一个函数,将字符串中的所有字符按ASCII 码值排序并返回。
答案:```pythondef sort_string(s):return ''.join(sorted(s))```五、论述题1. 论述计算机硬件和软件之间的关系及其对计算机系统性能的影响。
答案:计算机硬件是计算机系统的物质基础,包括CPU、内存、硬盘等,而软件则是运行在硬件上的程序和数据。
ACM软件大赛之编程大赛题目(附部分答案)
ACM 软件大赛之编程大赛比赛注意事项:l 比赛时间为3小时(小时(180180分钟);比赛分两个阶段:第一阶段限时30分钟,完成公示的3题,第二阶段限时150分钟(事先完成第一阶段题目的小组可提前进入第二阶段); l 比赛第一阶段的3道题目将在前期宣传中告知参赛选手,比赛第二阶段的题目将由赛事主席当场公布竞赛题目;主席当场公布竞赛题目;l 前两阶段题目分为三个分值(前两阶段题目分为三个分值(55分、分、1010分、分、1515分),第一阶段3道公示题都为5分;第二阶段总共15道题,根据不同的难度分值不同,分别为5道5分题,分题,55道10分题,分题,55道15分题;第一阶段参赛队员不可参考任何相关资料;第二阶段参赛队员可以携带诸如书,如书,手册,程序清单等参考资料。
手册,程序清单等参考资料。
手册,程序清单等参考资料。
比赛过程中队员不得携带任何电子媒质的资料;参比赛过程中队员不得携带任何电子媒质的资料;参赛者可以选择自己擅长的语言(赛者可以选择自己擅长的语言(C,C++,JAVA C,C++,JAVA 等等)进行编写等等)进行编写l 考虑到大一和大二学生的知识掌握程度,大一参加选手一开始就会有10分的分数,最后总分是由所做题目及初始的10分相加得到。
分相加得到。
l 每组队员根据安排使用电脑,小组人数为两人的使用一台电脑,超过两人的使用两台电脑,每台的电脑配置完全相同;脑,每台的电脑配置完全相同;l 各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分;各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分; l 如在比赛中发现作弊等行为,将取消比赛资格。
如在比赛中发现作弊等行为,将取消比赛资格。
第一阶段公示题目:题目一:(5分) 打印以下图形,纵遵从字母顺序,行字符数遵从斐波那契数列ABCCDDD EEEEEFFFFFFFFGGGGGGGGGGGGG#include<iostream>int f(int x){int a = 1 , b = 0;int max_ = x;int sum = 0; for(int i = 0; i < max_ ; i++){sum = a + b;a = b;b = sum;}return sum;}void loop_print(int num,char chr){for(int i = 0; i < num ;i++)std::cout<<chr;std::cout<<"\n";}int main(){int line_max = 7;char chr = 'A';for(int line = 0; line < line_max; line++){loop_print(f(line+1),chr);chr++;}return 0;}题目二:(5分)有个电子钟,12点显示为12:00(即12小时制),那么请问一天24时间内,出现连续3个相同数字的钟点有几个?#include<iostream>using namespace std;bool check(int me){int h= me/100;int m= me-100*h;return h<=12&&m<=59&&h>0?true:false;//12小时制小时制}int main(){int me=0;int j(0);//总计数器总计数器while( me<1270){//max 12:59int t= me;int n[4];for(int i=0;i<4;i++){n[i]=t%10;t /= 10;}if(n[1]==n[2]&&(n[0]==n[1]||n[3]==n[1])&&check( me)){//cout<<n[3]<<n[2]<<":"<<n[1]<<n[0]<<"\n";//testj++;me++;}cout<<"total: "<<j*2<<endl;}题目三:(5分)10进制的四位数中有几个符合如下特征:将其分别表示为16进制、10进制、12进制,在每种状态下,分别将各个位上的数相加,能得到3个相等10进制数。
ACM题目、测试用例及参考答案汇编——一次ACM协会内部测试
ACM题目、测试用例及参考答案汇编——一次ACM协会内部测试第一题:梦境是虚幻吗?时间限制:3000ms 内存限制:65535KB 难度:★★描述《盗梦空间》是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中的时间过得快得多,这里假设现实中的3分钟,在梦里就是1小时。
然而,Cobb他们利用强效镇静剂,可以从第一层梦境进入第二层梦境,甚至进入三层,四层梦境,每层梦境都会产生同样的时间加速效果。
那么现在给你Cobb在各层梦境中经历的时间,你能算出现实世界过了多长时间吗?比如,Cobb先在第一层梦境待了1个小时,又在第二层梦境里待了1天,之后,返回第一层梦境之后立刻返回了现实。
那么在现实世界里,其实过了396秒(6.6分钟)输入第一行输入一个整数T(0<=T<=100),表示测试数据的组数。
每组测试数据的第一行是一个数字M(3<=M<=100)随后的M行每行的开头是一个字符串,该字符串如果是"IN" 则Cobb向更深层的梦境出发了,如果是字符串"OUT"则表示Cobb从深层的梦回到了上一层。
如果是首字符串是"STAY"则表示Cobb在该层梦境中停留了一段时间,本行随后将是一个整数S表示在该层停留了S分钟(1<=S<=10000000)。
数据保证在现实世界中,时间过了整数秒。
输出对于每组测试数据,输出现实世界过的时间(以秒为单位)。
样例输入16INSTAY 60INSTAY 1440OUTOUT样例输出396测试输入106INSTAY 60INSTAY 1440OUTOUT6INININOUTOUTOUT7INININSTAY 0 OUTOUTOUT2INSTAY 203INSTAY 0 OUT3INSTAY 10 OUT4INSTAY 10 STAY 10 OUT5INSTAY 20 STAY 20 OUT STAY 120 10INSTAY 20 STAY 20 INSTAY 1440STAY 1440OUTSTAY 120OUTSTAY 11STAY 50测试输出39660306073209723000参考代码:#include<stdio.h>int main(){int n;char a[5];scanf("%d",&n);while(n--){int m,i,b=1,c,time=0;scanf("%d",&m);for(i=0;i<m;i++){scanf("%s",&a);if(a[0]=='I') b*=20;else if(a[0]=='S') {scanf("%d",&c);time+=c*60/b;} else if(a[0]=='O') b/=20;}printf("%d\n",time);}return 0;}第二题:独木舟过河时间限制:3000ms 内存限制:65535KB 难度:★★描述进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。
ACM程序大赛选拔初赛试题 - 参考答案
《解方程》参考答案:
#include <stdio.h> #include <math.h> void main() {
int a,b,c,d; float x1,x2; scanf("%d %d %d %d",&a,&b,&c,&d); if(a==0) printf("x=%.2f",(float)(d-c)/b); else {
float dr=b*b-4*a*c; c-=d; if(dr>0) {
x1=(-b-sqrt(dr))/(2*a); x2=(-b+sqrt(dr))/(2*a); printf("x1=%.2f x2=%.2f\n",x1,x2); } if(dr==0) { x1=(-b+sqrt(dr))/(2*a); printf("x=%.2f\n",x1); } if(dr<0) printf("no answer\n"); } }
3、计算器 (难度系数:★★★☆☆)
题意:输入一个没有答案的算术式子,用计算机求出输出它的结果。 输入:一个字符串形如“a#b=”(#代表运算符号)。注意:输入的字符串中 a,b 代表的数 都为自然数。而且 a,b 的范围都是从 0 到 999。 输出:字符串对应的算式的答案。答案需保留两位有效数字。若无答案则输出“error” 示例:
输出:第1 行是选出顺序,第2 行是两名幸运儿的开始位置(按升序排列),位置编号之 间用一个空格分开。
示例:
输入(只选一组) 3 5 12
输出(幸运儿的位置)
2 13 241 53 2 4 6 8 10 12 3 7 11 5 19
计算机学院ACM设计大赛初赛试题
计算机学院ACM设计大赛初赛试题问题一:给定一个N位的二进制串b1 b2 …bN-1 bN将该串做旋转,即将b1移到bN后面,得到一个新的二进制串:b2 …bN-1 bN b1对新的二进制串再做旋转,得二进制串b3 b4 …bN-1 bN b1 b2重复旋转操作操作,可得N个二进制串,对这N个串排序,可得一个N*N的矩阵问:给定这种矩阵的最后一列,求出矩阵的第一行。
对于上面的例子,给出1 0 0 1 0,要你的程序输出0 0 0 1 1。
原题描述:Binary codesTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4881 Accepted: 1847DescriptionConsider a binary string (b1…b N) with N binary digits. Given such a string, the matrix of Figure 1 is formed from the rotated versions of the string.b 1b2…b N−1b Nb 2b3…b N b1…b N−1b N…b N−3b N−2b N b1…b N−2b N−1Figure 1. The rotated matrixThen rows of the matrix are sorted in alphabetical order, where ‘0’ is before ‘1’. You are to write a program which, given the l ast column of the sorted matrix, finds the first row of the sorted matrix.As an example, consider the string (00110). The sorted matrix is0 0 0 1 10 0 1 1 00 1 1 0 01 0 0 0 11 1 0 0 0and the corresponding last column is (1 0 0 1 0). Given this last column your program should determine the first row, which is (0 0 0 1 1).InputThe first line contains one integer N≤ 3000, the number of binary digits in the binary string. The second line contains N integers, the binary digits in the last column from top to bottom.OutputThe first line contains N integers: the binary digits in the first row from left to right. Sample Input51 0 0 1 0Sample Output0 0 0 1 1问题二:某地的交通网由路口和公路构成:两个路口之间最多有一条公路,公路两个方向的行驶时间相同。
ACM选拔赛试题
ACM选拔赛试题
注意:程序文件命名为:Test+题号
1、已知四位数3025有一个特殊性质:它的前两位数30和后两位数25的和是55,而55的平方刚好等于该数(55*55=3025),编写一个程序求出所有的具有这种性质的四位数。
2、有的三位数很独特,它们每位上的数字互不相同且都不大于7,特别是十位数正好是百位数和个位数之差,编写程序求所有这样的三位数。
3、某电台组织一次智力竞赛,计划奖励30人。
准备了50件奖品,得一等奖者可得3件,二等奖可得2件,三等奖可得1件。
希望把所有奖品都发到获奖者手中,请编程找出所有的设奖方案(即各等奖各有多少人)。
4、修改31743的某一位数字,使之成为823的倍数。
5、在自然数中,各位数字之和的11倍正好等于自身的自然数只有一个,编写程序请找出这个自然数。
6、小明的妈妈是负责分发单位工资的。
为了使分发时有足够的零钞,同时又尽量不使每个人领到的钱太零碎。
每个月她都要计算出各种面值的钞票(100元、50元、20元、10元、5元、2元、1元)各需多少张。
假设每个人的工资都是整数元,你能否为她设计一个程序,从键盘输入10个人的工资,再计算各种面值的钞票各需多少张?
7、4名专家对四款赛车进行评论:
A说:2号赛车是最好的。
B说:4号赛车是最好的。
C说:3号赛车不是最佳赛车。
D说:B说错了。
事实上,只有一款赛车最佳,且只有一名专家说对了,其他3人说错了,请编程输出最佳赛车的车号,以及哪一位专家说对了。
ACM比赛试题
The 35th ACM-ICPC Asia Regional Contest (Hangzhou)Contest SectionOctober 24, 2010Sponsored by IBM & AlibabaZhejiang Sci-Tech UniversityThis problem set should contain 10 problems on numbered 24 pages. Please inform a runner immediately if something is missing from your problem set.Problem A. Naughty fairiesDescriptionOnce upon a time, there lived a kind of fairy in the world. Those fairies could hear the voice of fruit trees, and helped people with a harvest. But people did n’t know that fruits are also those fairies’ favorite food. After the fairies ate people’s fruits, they always did something to cover it up.One day a little fairy named Lily flew into an orchard and found a large peach tree. Hungry as Lily was, she started eating without thinking until her stomach was full. In the fairy world, when a fairy ate the fruits in a fruit tree, sometimes the fruit tree would feel honored and bore more fruits immediately. That’s why sometimes the number of fruits in a tree got increased after a fairy ate fruits of that tree.But the fairies didn’t want people to find out weird things such as fruits become more or less suddenly. Lily decided to use a magic spell so that the orchard owner couldn’t find the change of the number of p eaches.Suppose there were N peaches on a tree originally and there were M peaches left after Lily was full. M may be greater than, less than or equal to N. All M peaches were visible at first, and Lily wanted to make an illusion so that exactly N peaches are visible.Lily can do 3 kinds of spell to change the total visible number of peaches:1) “PAPADOLA”:This spell would increase the number of visible peaches by one.2) “EXPETO POTRONUM”:This spell would double the number of visible peaches.3) “SAVIDA LOHA”:This spell would decrease the number of visible peaches by one. Each spell would take one minute and Lily wanted to finish as fast as possible. Now please tell Lily the least time she needs to change the number of visible peaches to N.InputThere are several test cases, ended by “0 0”.For each test case, there are only one line containing two numbers separated by a blank, N and M, the original numbers of peaches and the numbers of peaches left(0<N,M<10500).There is no leading zero.OutputFor each test case, you should output just a number K indicating the minimum time (in minutes) Lily needed to finish her illusion magic.Sample Input5 21 9986 320 0Sample Output29812Problem B. Prison BreakDescriptionRompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 needs an optimal plan and he contacts you, one of his human friends, for help.The jail area is a rectangle contains n×m little grids, each grid might be one of the following:1) Empty area, represented by a capital letter ‘S’.2) The starting position of Micheal#1, represented by a capital letter ‘F’.3) Energy pool, represented by a capital letter ‘G’. When entering a n energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’s batt ery will become FULL and the energy pool will become an empty area. Of course, passing an energy pool without using it is allowed.4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor.5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off.In order to escape from the jail, Micheal#1 need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost 1 unit of energy and only moving operation costs energy. Of course, Micheal#1 cannot move when his battery contains no energy.The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal#1 needs to make his battery as small as possible, and still large enough to hold all energy he need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.InputInput contains multiple test cases, ended by 0 0. For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.OutputFor each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.Sample Input5 5GDDSSSSSFSSYGYSSGSYSSSYSS0 0Sample Output4Problem C. To Be an Dream Architect DescriptionThe “dream architect” is the key role in a team of “dream extractors” who enter other’s dreams to steal secrets. A dream architect is responsible for crafting the virtual world that the team and the target will dream into. To avoid the target noticing the world is artificial, a dream architect must have powerful 3D imagination.Cobb uses a simple 3D imagination game to test whether a candidate has the potential to be an dream architect. He lets the candidate imagine a cube consisting of n×n×n blocks in a 3D coordinate system as Figure 1. The block at bottom left front corner is marked (1, 1, 1) and the diagonally opposite block is marked (n, n, n). Then he tells the candidate that the blocks on a certain line are eliminated. The line is always parallel to an axis. After m such block eliminations, the candidate is asked to tell how many blocks are eliminated. Note that one block can only be eliminated once even if it is on multiple lines.Here is a sample graph according to the first test case in the sample input:InputThe first line is the number of test cases.In each test case, the first line contains two integers n and m( 1 <= n <= 1000, 0 <= m <= 1000).,meaning that the cube is n x n x n and there are m eliminations.Each of the following m lines represents an elimination in the following format:axis_1=a, axis_2=bwhere axis_i (i=1, 2) is ‘X’ or ‘Y’, or ‘Z’ and axis_1 is not equal to axis_2. a and b ar e 32-bit signed integers.OutputFor each test case output the number of eliminated blocks.Sample Input23 2Y=1,Z=3X=3,Y=110 2X=3,Y=3Y=3,Z=3Sample Output519Problem D. GomokuDescriptionYou are probably not familiar with the title, “Gomoku”, but you must have played it a lot. Gomoku is an abstract strategy board game and is also called Five in a Row, or GoBang. It is traditionally played with go pieces (black and white stones) on a go board (19x19 intersections). Nowadays, standard chessboard of Gomoku has 15x15 intersections. Black plays first, and players alternate in placing a stone of their color on an empty intersection. The winner is the first player to get an unbroken row of five or more stones horizontally, vertically, or diagonally.For convenience, we coordinate the chessboard as illustrated above. The left-bottom intersection is (0,0). And the bottom horizontal edge is x-axis, while the left vertical line is y-axis.I am a fan of this game, actually. However, I have to admit t hat I don’t have a sharp mind. So I need a computer program to help me. What I want is quite simple. Given a chess layout, I want to know whether someone can win within 3 moves, assuming both players are clever enough. Take the picture above for example. There are 31 stones on it already, 16 black ones and 15 white ones. Then we know it is white turn. The white player must place a white stone at (5,8). Otherwise, the black player will win next turn. After that, however, the white player also gets a perfect situation that no matter how his opponent moves, he will win at the 3rd move.So I want a program to do similar things for me. Given the number of stones and positions of them, the program should tell me whose turn it is, and what will happen within 3 moves.InputThe input contains no more than 20 cases.Each case contains n+1 lines which are formatted as follows.nx1 y1 c1x2 y2 c2......x n y n c nThe first integer n indicates the number of all stones. n<=222 which means players have enough space to place stones. Then n lines follow. Each line contains three integers: x i and y i and c i. x i and y i are coordinates of the stone, and ci means the color of the stone. If c i=0 the stone is white. If c i=1 the stone is black. It is guaranteed that 0<=x i,y i<=14, and c i=0 or 1. No two stones are placed at the same position. It is also guaranteed that there is no five in a row already, in the given cases.The input is ended by n=0.OutputFor each test case:First of all, the program should check whose turn next. Le t’s call the player who will move next “Mr. Lucky”. Obviously, if the number of the black stone equals to the number of white, Mr. Lucky is the black player. If the number of the black stone equals to one plus the numbers of white, Mr. Lucky is the white player. If it is not the first situation or the second, print “Invalid.”A valid chess layout leads to four situations below:1)Mr. Lucky wins at the 1st move. In this situation, print :Place TURN at (x,y) to win in 1 move.“TURN” must be replaced by “black” or “white” according to the situation and (x,y) is the position of the move. If there are different moves to win, choose theone where x is the smallest. If there are still different moves, choose the one where y is the smallest.2)Mr. Lucky’s opp onent wins at the 2nd move. In this situation, print:Lose in 2 moves.3)Mr. Lucky wins at the 3rd move. If so, print:Place TURN at (x,y) to win in 3 moves.“TURN” should replaced by “black” or “white”, (x,y) is the position where the Mr.Lucky should place a stone at the 1st move. After he place a stone at (x,y), no matter what his opponent does, Mr. Lucky will win at the 3rd step. If there are multiple choices, do the same thing as described in situation 1.4)Nobody wins within 3 moves. If so, print:Cannot win in 3 moves.Sample Input313 3 13 4 03 5 03 6 04 4 14 5 14 7 05 3 05 4 05 5 15 6 15 7 15 9 16 4 16 5 16 6 06 7 16 8 06 9 07 5 17 6 07 7 17 8 17 9 08 5 08 6 18 7 08 8 18 9 09 7 110 8 017 7 117 7 0Sample OutputPlace white at (5,8) to win in 3 moves. Cannot win in 3 moves.Invalid.Problem E. GunshotsDescriptionPresident Bartlet was shot! A group of terrorists shot to the crowd when President Bartlet waved to cheering people after his address. Many people were shot by the irrational bullets. Senior FBI agent Don Epps takes responsibility for this case. According to a series of crime scene investigation, including analyzing shot shells, replaying video from closed-circle television and collecting testimony by witnesses, Don keeps all the information about where and how the terrorists shot to crowd, as well as the location of every single person when the gun shoot happened. Now he wants to know how many gunshot victims are there in this case.Imagine that each target person can be regarded as a polygon (can be concave or self-intersecting) and each gunshot can be regarded as a half-line. The bullet will be stopped by the first person it shoots. A person can be shot in three ways:To simplify the problem, we assume that any two polygons can be completely separated by a line. Also each start point of the gunshot can be separated from each polygon by a line. Now given M people and N gunshots, please work out which person has been shot by each bullet.InputThere are multiple test cases in the input. The first line of the input file is an integer T demonstrating the number of test cases. (T<=10).For each test case, the first line is an integer N, representing the number of people (polygons). Following lines demonstrates the polygons. For the i th polygon (0<=i<N), the first line is an integer Q i , representing the number of edges of this polygon. In each of the following Q i lines, there are two real numbers x i and y i representing a point. Every pair of adjacent points demonstrate an edge of this polygon (i.e. (x i , y i ) to (x i+1, y i+1) is an edge, in which 0<=i<Q i -1), and (x Qi-1, y Qi-1) to (x 0, y 0) also demonstrates an edge of this polygon.PersonPerson Person 1. Normal shot 2. The bullet’s path isparallel to an edge 3. The bullet’s path is tangent to an vertexThen there is a line contains an integer M representing the number of gunshots. In the following M lines, each line contains four real numbers x, y, dx and dy, representing the start point (x, y) and direction vector (dx, dy) of that gunshot.In all test cases, we assume that 0< N<=100, 0<Q i<=1000, 0<M<=10000.OutputFor each test case, output contains M lines and the i th line demonstrates the result of the i th gunshot.If the i th gunshot shoots the j th polygon, the i th line contains “HIT j”, otherwise it contains a word “MISS” (means that it does not shoot any target). The polygons are numbered in the order of their appearance in the input file, and the numbers start from 0.At the end of each test case, please output a single line with “*****”.Sample Input1140 01 10 11 02-1 0 1 0-2 0 -1 0Sample OutputHIT 0MISS*****HintThe figure of the first case in the samples is as follows:Problem F. Rotational PaintingDescriptionJosh Lyman is a gifted painter. One of his great works is a glass painting. He creates some well-designed lines on one side of a thick and polygonal glass, and renders it by some special dyes. The most fantastic thing is that it can generate different meaningful paintings by rotating the glass. This method of design is called “Rotatio nal Painting (RP)” which is created by Josh himself.You are a fan of Josh and you bought this glass at the astronomical sum of money. Since the glass is thick enough to put erectly on the table, you want to know in total how many ways you can put it so that you can enjoy as many as possible different paintings hiding on the glass. We assume that material of the glass is uniformly distributed. If you can put it erectly and stably in any ways on the table, you can enjoy it.More specifically, if the polygonal glass is like the polygon in Figure 1, you have just two ways to put it on the table, since all the other ways are not stable. However, the glass like the polygon in Figure 2 has three ways to be appreciated.Pay attention to the cases in Figure 3. We consider that those glasses are not stable.InputThe input file contains several test cases. The first line of the file contains an integer T representing the number of test cases.For each test case, the first line is an integer n representing the number of lines of the polygon. (3<=n<=50000). Then n lines follow. The i th line contains two real number x i and y i representing a point of the polygon. (x i, y i) to (x i+1, y i+1) represents a edge of the polygon (1<=i<n), and (x n,y n) to (x1, y1) also represents a edge of the polygon. The input data insures that the polygon is not self-crossed.OutputFor each test case, output a single integer number in a line representing the number of ways to put the polygonal glass stably on the table.Sample Input240 0100 099 11 160 00 101 101 110 110 0Sample Output23HintThe sample test cases can be demonstrated by Figure 1 and Figure 2 in Description part.Problem G. Traffic Real Time Query System DescriptionCity C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, the mayor plans to build a RTQS (Real Time Query System) to monitor all traffic situations. City C is made up of N crossings and M roads, and each road connects two crossings. All roads are bidirectional. One of the important tasks of RTQS is to answer some queries about route-choice problem. Specifically, the task is to find the crossings which a driver MUST pass when he is driving from one given road to another given road.InputThere are multiple test cases.For each test case:The first line contains two integers N and M, representing the number of the crossings and roads.The next M lines describe the roads. In those M lines, the i th line (i starts from 1)contains two integers X i and Y i, representing that road i connects crossing X i and Y i (X i≠Y i).The following line contains a single integer Q, representing the number of RTQs. Then Q lines follows, each describing a RTQ by two integers S and T(S≠T) meaning that a driver is now driving on the road s and he wants to reach road t . It will be always at least one way from road s to road t.The input ends with a line of “0 0”.Please note that: 0<N<=10000, 0<M<=100000, 0<Q<=10000, 0<X i,Y i<=N, 0<S,T<=M OutputFor each RTQ prints a line containing a single integer representing the number of crossings which the driver MUST pass.Sample Input5 61 22 33 44 53 522 32 40 0Sample Output 01Problem H. National Day ParadeDescriptionThere are n×n students preparing for the National Day parade on the playground. The playground can be considered as a n×m grid. The coordinate of the west north corner is (1,1) , and the coordinate of the east south corner is (n,m).When training, every students must stand on a line intersection and all students must form a n×n square. The figure above shows a 3×8 playground with 9 students training on it. The thick black dots stand for the students. You can see that 9 students form a 3×3 square.After training, the students will get a time to relax and move away as they like. To make it easy for their masters to control the training, the students are only allowed to move in the east-west direction. When the next training begins, the master would gather them to form a n×n square again, and the position of the square doesn’t matter. Of course, no student is allowed to stand outside the playground.You are given the coordinates of each student when they are having a rest. Your task is to figure out the minimum sum of distance that all students should move to form a n×n square.InputThere are at most 100 test cases.For each test case:The first line of one test case contain two integers n,m. (n<=56,m<=200)Then there are n×n lines. Each line contains two integers, 1<=X i<=n,1<= Y i<=m indicating that the coordinate of the i th student is (X i , Y i ). It is possible for more than one student to stand at the same grid point.The input is ended with 0 0.OutputYou should output one line for each test case. The line contains one integer indicating the minimum sum of distance that all students should move to form a n×n square. Sample Input2 1682 1011 1271 1052 900 0Sample Output41Problem I. SearchlightsDescriptionThere is a piece of grids land of size n×m. Chandler and his team take responsibility to guard it. There are some searchlights on some pieces and each of them has a capability to lighten a distance towards four directions: north, south, east and west. Different searchlight has different lightening capability shown in levels. Searchlight with level k means that it can lighten k grids (including the gird that the searchlight stands in) along any of the four directions. Shown in following figure, there is a searchlight of level 3 and the shadow grids are ones that can be lightened by it. Particularly, searchlight of level 1 means that it can only lighten the grid in which the searchlight stands.Figure: A searchlight of Level 3Each searchlight has a maximum level. You can decrease a searchlight’s level to save the energy. A searchlight whose maximum level is k can be turned to level k, k-1, k-2, …, 1 and 0. Level 0 means turning off the searchlight.A grid is well-guarded if and only if at least one of the following two conditions is satisfied:1.There is a searchlight in this grid, and it is not switched to level 0 (the light is on).2.The grid is lightened by at least two searchlights. One lightens it in horizontaldirection (east or west), and another lightens it in vertical direction (north or south).Chandler asks you to help finding a solution that he can turn on some of the searchlights so that:1.All the grids are well-guarded.2.All the searchlights turned on are in a same level.3.That same level mentioned above is as small as possible.More specifically, if you choose a same level Q, then all the searchlights whose maximum level are less than Q have to be turned off. Please help him to find a solution with the minimum same level.InputThe input file contains several test cases.For each test case, the first line is two integers n and m, representing a grids land of size n×m. (0<n<=100, 0<m<=10000). Following n lines describe an n×m matrix in which a i,j means the maximum level of the searchlight in grid (i, j). a i,j can be zero, which means there is no searchlight on that grid. For all the cases, a i, j<=10000.The input file ends with a line containing two zeros.OutputFor each test case, output a single line with an integer, representing the minimum level you have found. If there is no such a solution, output “NO ANSWER!”Sampl e Input2 20 23 02 20 21 00 0Sampl e Output2NO ANSWER!Problem J. Infinite monkey theorem DescriptionCould you imaging a monkey writing computer programs? Surely monkeys are smart among animals. But their limited intelligence is no match for our human beings. However, there is a theorem about monkeys, and it states that monkeys can write everything if given enough time.The theorem is called “Infinite monkey theorem”. It states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type any given text, which of course includes the programs you are about to write (All computer programs can be represented as text, right?).It’s very easy to prove this theorem. A little calculation will show you that if the monkey types for an infinite length of time the probability that the output contains a given text will approach 100%.However, the time used is too long to be physically reasonable. The monkey will not be able to produce any useful programs even if it types until the death of the universe. To verify this and ensure that our human beings are not replaceable by monkeys, you are to calculate the probability that a monkey will get things right.InputThere will be several test cases.Each test case begins with a line containing two integers n and m separated by a whitespace (2<=n<=26, 1<=m<=1000). n is the number of keys on the typewriter and the monkey will hit these keys m times. Thus the typewriter will finally produce an output of m characters.The following n lines describe keys on the typewriter. Each line has a lower case letter and a real number separated by a whitespace. The letter indicates what the typewriter will produce if the monkey hits that key and the real number indicates the probability that the monkey will hit this key. Two hits of the monkey are independent of each other (Two different hits have the same probability for a same key), and sum of all the probabilities for each key is ensured to be 1.The last line of the test case contains a word composed of lower case letters. The length of the word will be less than or equal to 10.The input will end with a line of two zeros separated by a whitespace. This line should not be processed.OutputFor each test case, output one line containing the probability that the given word will appear in the typewriter’s output. The output should be in percentage format and numbers should be rounded to two digits after the decimal point.Sampl e Input4 10w 0.25o 0.25r 0.25d 0.25word2 10a 1.0b 0.0abc2 100a 0.312345b 0.687655abab0 0Sampl e Output2.73%0.00%98.54%。
大一acm竞赛试题及答案
大一acm竞赛试题及答案一、选择题(每题5分,共20分)1. 下列哪个算法的时间复杂度为O(n^2)?A. 快速排序B. 归并排序C. 插入排序D. 冒泡排序答案:C2. 在C++中,下列哪个关键字用于定义类?A. structB. classC. unionD. enum答案:B3. 下列哪个数据结构适合用于实现稀疏矩阵?A. 顺序存储B. 链式存储C. 压缩存储D. 散列存储答案:C4. 在图论中,下列哪个算法用于寻找最短路径?A. 深度优先搜索B. 广度优先搜索C. 迪杰斯特拉算法D. 弗洛伊德算法二、填空题(每题5分,共20分)1. 在二叉树的遍历算法中,______遍历会先访问根节点。
答案:前序2. 哈希表的冲突解决方法之一是______。
答案:链地址法3. 在数据库中,用于实现一对多关系的表结构是______。
答案:外键4. 动态规划算法的核心是______。
答案:状态转移方程三、编程题(每题30分,共60分)1. 编写一个函数,实现对一个整数数组进行排序,并返回排序后的数组。
答案:```pythondef sort_array(arr):arr.sort()return arr```2. 编写一个函数,实现计算给定整数n的阶乘。
答案:```pythondef factorial(n):if n == 0:return 1return n * factorial(n - 1)```四、算法题(每题30分,共30分)1. 给定一个整数数组,请设计一个算法找出数组中第二大的数。
答案:```pythondef find_second_max(nums):first_max = second_max = float('-inf')for num in nums:if num > first_max:second_max = first_maxfirst_max = numelif num > second_max and num != first_max:second_max = numreturn second_max```。
acm数学竞赛试题及答案
acm数学竞赛试题及答案# 题目一:数列问题问题描述:给定一个数列 \( a_1, a_2, a_3, \ldots, a_n \),数列中每个元素都是正整数,且满足 \( a_i = a_{i-1} + a_{i-2} \) 对于所有\( i \geq 3 \)。
如果 \( a_1 = 1 \) 且 \( a_2 = 1 \),请找出数列的第 \( n \) 项。
解答:根据题意,这是一个斐波那契数列。
第 \( n \) 项的值可以通过递归关系计算得出。
对于 \( n \) 的值,可以使用以下递归公式:\[ a_n = a_{n-1} + a_{n-2} \]其中,\( a_1 = 1 \) 和 \( a_2 = 1 \)。
因此,数列的前几项为 1, 1, 2, 3, 5, 8, 13, 21, ...。
对于任意的 \( n \),可以通过递归或动态规划方法计算出 \( a_n \)。
# 题目二:组合问题问题描述:从 \( n \) 个不同的元素中选择 \( k \) 个元素的所有可能组合的个数是多少?解答:这个问题可以通过组合数学中的二项式系数来解决。
从 \( n \) 个不同元素中选择 \( k \) 个元素的组合数 \( C(n, k) \) 可以用以下公式计算:\[ C(n, k) = \frac{n!}{k!(n-k)!} \]其中,\( n! \) 表示 \( n \) 的阶乘。
# 题目三:几何问题问题描述:在一个直角坐标系中,给定三个点 \( A(x_1, y_1) \),\( B(x_2, y_2) \) 和 \( C(x_3, y_3) \)。
如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 是垂直的,求证 \( A \) 是直角三角形 \( ABC \) 的直角顶点。
解答:如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 垂直,那么它们的数量积(点积)应该为零。
河南acm试题及答案
河南acm试题及答案河南ACM试题及答案1. 问题描述编写一个程序,计算给定整数序列中,满足条件的子序列数量。
条件为:子序列中任意两个相邻元素的差的绝对值不超过1。
2. 输入格式第一行包含一个整数N,表示序列的长度,1 <= N <= 100000。
第二行包含N个整数,表示序列中的元素,-1000000 <= 数字 <= 1000000。
3. 输出格式输出满足条件的子序列数量。
4. 样例输入```51 2 3 2 1```5. 样例输出```5```6. 问题分析本题需要对给定的整数序列进行遍历,使用动态规划的方法来计算满足条件的子序列数量。
设dp[i]表示以第i个元素结尾的满足条件的子序列数量,那么dp[i]的值可以通过dp[i-1](当前元素与前一个元素相等或相邻)和dp[i-2](当前元素与前两个元素相邻)计算得出。
7. 算法实现```pythondef countSubsequences(nums):dp = [0] * (len(nums) + 1)dp[0] = 1for i in range(1, len(nums) + 1):for j in range(i):if abs(nums[i-1] - nums[j-1]) <= 1:dp[i] += dp[j]return sum(dp)```8. 测试用例对于样例输入,程序应输出5,表示有5个满足条件的子序列。
9. 注意事项- 确保输入数据的格式正确。
- 考虑边界条件,如序列长度为1时的情况。
- 动态规划数组的大小应根据序列长度动态调整。
ACM程序设计竞赛例题
ACM程序设计竞赛例题备战ACM资料习题1. 0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。
从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。
对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。
程序如下:#includevoid readdata();void search(int);void checkmax();void printresult();int c=35, n=10; //c:背包容量;n:物品数int w[10], v[10]; //w[i]、v[i]:第i件物品的重量和价值int a[10], max; //a数组存放当前解各物品选取情况;max:记录最大价值//a[i]=0表示不选第i件物品,a[i]=1表示选第i件物品int main() {readdata(); //读入数据search(0); //递归搜索printresult();}void search(int m){if(m>=n)checkmax(); //检查当前解是否是可行解,若是则把它的价值与max 比较else{a[m]=0; //不选第m件物品search(m+1); //递归搜索下一件物品a[m]=1; //不选第m件物品search(m+1); //递归搜索下一件物品}}void checkmax(){int i, weight=0, value=0;for(i=0;i<n;i++)< p="">{if(a[i]==1) //如果选取了该物品{weight = weight + w[i]; //累加重量value = value + v[i]; //累加价值}}if(weight<=c) //若为可行解if(value>max) //且价值大于maxmax=value; //替换max}void readdata(){int i;for(i=0;i<n;i++)< p="">scanf("%d%d",&w[i],&v[i]); //读入第i件物品重量和价值}void printresult(){printf("%d",max);}2.装载问题有两艘船,载重量分别是c1、c2,n个集装箱,重量是wi (i=1…n),且所有集装箱的总重量不超过c1+c2。
ACM程序设计竞赛例题[1]
A C M程序设计竞赛例题[1]-CAL-FENGHAI.-(YICAI)-Company One1备战ACM资料习题1.0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。
从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。
对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。
程序如下:#include <>void readdata();void search(int);void checkmax();void printresult();int c=35, n=10; ");printf("\n");}printf("\n");}6.素数环问题把从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数。
分析:用回溯算法,考察所有可能的排列。
程序如下:#include <>#include <>void search(int);void init(); 表示空格;’X’表示墙。
程序如下:#include <>#include <>void search(int,int);int canplace(int,int);void readdata(); Floodfill给一个20×20的迷宫和一个起点坐标,用广度优先搜索填充所有的可到达的格子。
提示:参考第2题。
2. 电子老鼠闯迷宫如下图12×12方格图,找出一条自入口(2,9)到出口(11,8)的最短路本题给出完整的程序和一组测试数据。
状态:老鼠所在的行、列。
程序如下:#include<>void readdata();a[i][j]=0; ....注:测试数据可在运行时粘贴上去(点击窗口最左上角按钮,在菜单中选则“编辑”/“粘贴”即可)。
acm大学生程序试题及答案
acm大学生程序试题及答案1. 题目:字符串反转描述:给定一个字符串,编写一个函数来将字符串中的字符按相反的顺序重新排列。
输入:一个字符串输出:反转后的字符串答案:```pythondef reverse_string(s):return s[::-1]```2. 题目:寻找最大数描述:给定一个整数数组,找出数组中的最大数。
输入:一个整数数组输出:数组中的最大数答案:```pythondef find_max(nums):return max(nums)```3. 题目:两数之和描述:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的索引(从1开始计数)。
输入:一个整数数组和一个目标值输出:两个数的索引,如果没有则返回空数组答案:```pythondef two_sum(nums, target):num_to_index = {}for i, num in enumerate(nums):complement = target - numif complement in num_to_index:return [num_to_index[complement] + 1, i + 1] num_to_index[num] = ireturn []```4. 题目:无重复字符的最长子串描述:给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
输入:一个字符串输出:最长子串的长度答案:```pythondef length_of_longest_substring(s):char_map = {}start = max_length = 0for end in range(len(s)):if s[end] in char_map:start = max(start, char_map[s[end]] + 1)char_map[s[end]] = endmax_length = max(max_length, end - start + 1)return max_length```5. 题目:整数转罗马数字描述:将一个整数转换为罗马数字。
acm试题及答案
acm试题及答案ACM试题及答案试题 1: 给定一个整数数组,请找出数组中第二大的数。
答案:1. 对数组进行排序。
2. 数组排序后,倒数第二个元素即为第二大的数。
试题 2: 编写一个函数,计算给定字符串中字符出现的次数。
答案:```pythondef count_characters(s):count_dict = {}for char in s:if char in count_dict:count_dict[char] += 1else:count_dict[char] = 1return count_dict```试题 3: 判断一个数是否为素数。
答案:1. 如果数小于2,则不是素数。
2. 从2开始到该数的平方根,检查是否有因数。
3. 如果没有因数,则该数是素数。
试题 4: 实现一个算法,将一个整数数组按照奇数在前,偶数在后的顺序重新排列。
答案:```pythondef rearrange_array(arr):odd = []even = []for num in arr:if num % 2 == 0:even.append(num)else:odd.append(num)return odd + even```试题 5: 给定一个链表,删除链表的倒数第n个节点。
答案:1. 遍历链表,找到链表的长度。
2. 再次遍历链表,找到倒数第n个节点的前一个节点。
3. 将前一个节点的next指针指向当前节点的下一个节点。
4. 如果当前节点是头节点,则更新头节点。
试题 6: 编写一个函数,实现字符串反转。
答案:```pythondef reverse_string(s):return s[::-1]```试题 7: 给定一个整数数组,找出数组中没有出现的最小正整数。
答案:1. 遍历数组,使用哈希表记录出现的数字。
2. 从1开始,检查每个数字是否在哈希表中。
3. 第一个不在哈希表中的数字即为答案。
试题 8: 实现一个算法,计算斐波那契数列的第n项。
福建农林大学思博杯第一届ACM程序设计大赛题目
福建农林大学思博杯第一届ACM程序设计大赛题目福建农林大学思博杯第一届ACM程序设计大赛题目1.多项式相乘(本题30分)(conv.cpp/c)【题目描述】编程实现若干个多项式相乘。
多项式的输入输出格式为:系数在前,指数在后,各项按指数递增排列,每个多项式输入时以两个0结束。
系数为0的项不输出。
例如:1+4X3-9X5输入格式为:1 0 0 1 0 2 4 3 0 4 -9 5 0 0或者 1 0 4 3 -9 5 0 0,其输出只能是:1 0 4 3 -9 5【输入】输入文件conv.in每行为一个多项式,多项式输入时以两个0结束。
数据为若干行的多项式,例如:1 0 1 1 0 01 0 -1 1 0 01 0 12 0 0表示(1+x)(1-x)(1+x2)【输出】输出文件conv.out包含1行,为上述多项式相乘结果。
上例的输出为:1 0 -1 4表示1-x4所有系数、指数均为整数(int类型)2.Total求和(本题60分)(total.cpp/c)【问题描述】编写程序实现从一字符串str中取出连续的数字作为一个正整数,计算所有这些正整数之和。
例如字符串str="abc123 x456,xy16639ghks-7890# zxy",按题目要求可以取出4个正整数:123,456,16639,7890。
这4个正整数之和为:25108【输入】输入文件total.in只有一行,是一个字符串(1≤字串长度≤500)。
【输出】输出文件total.out也只有一行,从输入一字符串中取出连续的数字作为一个正整数,输出所有这些正整数之和(1位整数≤和≤100位整数)。
3. FBI树(本题60分)(fbi.cpp/c)【问题描述】我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。
FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三种。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
取石子游戏
Time Limit:1S Memory Limit:1000K
Total Submit:505 Accepted:90
Description
有两堆石子,数量任意,可以不同。
游戏开始由两个人轮流取石子。
游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子;二是可以在两堆中同时取走相同数量的石子。
最后把石子全部取完者为胜者。
现在给出初始的两堆石子的数目,如果轮到你先取,假设双方都采取最好的策略,问最后你是胜者还是败者。
Input
输入包含若干行,表示若干种石子的初始情况,其中每一行包含两个非负整数a和b,表示两堆石子的数目,a和b都不大于1,000,000,000。
Output
输出对应也有若干行,每行包含一个数字1或0,如果最后你是胜者,则为1,反之,则为0。
Sample Input
2 1
8 4
4 7
Sample Output
1
跳蚤
Time Limit:1S Memory Limit:1000K
Total Submit:198 Accepted:44 Description
Z城市居住着很多只跳蚤。
在Z城市周六生活频道有一个娱乐节目。
一只跳蚤将被请上一个高空钢丝的正中央。
钢丝很长,可以看作是无限长。
节目主持人会给该跳蚤发一张卡片。
卡片上写有N+1个自然数。
其中最后一个是M,而前N个数都不超过M,卡片上允许
有相同的数字。
跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度。
而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物。
比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10个单位长度,然后再连向左跳3次,每次15个单位长度,最后再向右连跳3次,每次18个单位长度。
而持有卡片(12, 15, 18)的跳蚤,则怎么也不可能跳到距他左边一个单位长度的地方。
当确定N和M后,显然一共有M^N张不同的卡片。
现在的问题是,在这所有的卡片中,有多少张可以完成任务。
Input
两个整数N和M(N <= 15 , M <= 100000000)。
Output
可以完成任务的卡片数。
Sample Input
2 4
Sample Output
12
Hint
这12张卡片分别是:
(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4),
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4)。