寒假训练赛(四) 自我总结

时间:2017年1月25日 13:00 ~ 17:00


自我总结:自我感觉题目难度颇大,很多题都是赛后问队内诸神才懂的,并且是在赛后第二天的晚上才真正补完,而且感觉有些题哪怕赛后听了很多大佬的讲解,并能自己写出并AC,但并没有真正的吃透,知其然而不知其所以然,归根到底还是自己太弱了。


反省:因为题目很长而且过的人很少,我为了做好前面的题而放弃了1007,甚至题都没有读过,赛后补题时才发现是我比较擅长的迷宫题,而且比起以前做过的迷宫题而言难度并不大,果然赛后敲了不到半个小时,1次AC......qwq。这对我而言是一个很大的教训,果然做题不能跟着别人走,因为每个人擅长的知识领域不同,以后比赛哪怕再卡题也一定要把后面的题读一读qwq


题目:

1001:

A piece of cake(5-1001)

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 127   Accepted Submission(s) : 16
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  This problem is very simple, i.e. just a piece of cake for you, excellent programmers. You just need to calculate and output how many ways to put n different balls into m different boxes so that each box has at least k balls.

Input

  Input contains several test cases. Each of the test cases contains three integers in one line, n, m, k (1 <= n, m <= 15, 0 <= k <= 15). Input is terminated by three 0s, which should not be processed.

Output

  For each case, just print the result in one line. Heading zeros are forbidden. For example, 12 is legal output but 012 is not, 0 is legal but 00, and so on.

Sample Input

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

Sample Output

6
0
8

Source

2007省赛集训队练习赛(5)

坑啊.....高中自认为排列组合学得比较好,就一直从排列组合的角度来推公式,后来还真被我推出一个奇奇怪怪的公式,还过了样例(但赛后发现其实是错的),之后就开始了迷之循环:

WA -> 改公式 -> WA->改公式 -> WA -> ....

后来实在WA得太多了,又把题读了一遍又一遍,又注意到了一句“Heading zeros are forbidden”,提示前导零是错误的???这种非字符串的题可能出现前导零只有高精度了,然后又掉进了高精度的坑qwq(虽然测试了极限数据__int64没有爆,但当时已经WA得走火入魔了)


最后前前后后估计花了两个小时,没有AC.....qwq


赛后华哥给我讲了一下,才发现是个套路DP,记忆化搜索......qwq

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll c[20][20];
ll dp[20][20][20];

void init(void){
    memset(dp,-1,sizeof(dp));
    memset(c,0,sizeof(c));
    c[0][0] = 1;
    for(ll i = 1;i<=15 ;i++)
        c[i][0] = c[i][i] = 1;
    for(ll i=2 ;i<=15 ;i++){
        for(ll j=1 ;j<i ;j++){
            c[i][j] = c[i-1][j] + c[i-1][j-1];
        }
    }
}

ll dfs(ll m,ll n,ll k){
    ll res = 0;
    if(m == 1) return 1;
    if(dp[m][n][k] != -1) return dp[m][n][k];
    for(ll i=k ;i<= n-(m-1)*k ;i++){
        res += c[n][i] * dfs(m-1,n-i,k);
    }
    return dp[m][n][k] = res;
}

int main(){
    init();
    ll n,m,k;
    while(scanf("%I64d%I64d%I64d",&n,&m,&k) != EOF && (n||m||k)){
        if(n < m*k){
            printf("0\n");
            continue;
        }    
        ll ans = dfs(m,n,k);
        printf("%I64d\n",ans);
    }
    return 0;
}


1002:

Houses

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 44   Accepted Submission(s) : 33
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  There are several houses in a village. Houses are connected by roads, since building a road will cost much money, any two houses are connected by at most one road, and every road is guaranteed to connect two different houses.
  In the village, the smartest people always live in the most convenient house, and the most convenient house is a house connecting to the maximum number of houses. Now it is your task to find out the number of houses connecting to the most convenient house.

Input

  There’re multiple cases. The first line of each case contains two integers n and m (1≤n≤20, 0≤m≤20), indicating the number of houses and the number of roads, houses are numbered from 1 to n. Then for each of the following m lines, there are two different integers a and b (1≤a, b≤n), indicating that there is a road connecting house a and house b. Input is terminated by two 0’s.

Output

  For each case output the result in one line.

Sample Input

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

Sample Output

0
0
1
2
2


签到水题....才开始读题时还以为是最短路
#include <bits/stdc++.h>
using namespace std;

const int maxn = 25;
int Hash[maxn];

int main(){
	int n,m,i;
	while(scanf("%d%d",&n,&m) != EOF && (n||m)){
		memset(Hash,0,sizeof(Hash));
		int x,y;
		for(i=1 ;i<= m ;i++){
			scanf("%d%d",&x,&y);
			Hash[x]++;
			Hash[y]++;
		}
		int vmax = 0;
		int id = 0;
		for(i=1 ;i<maxn ;i++){
			if(Hash[i]>vmax){
				vmax = Hash[i];
				id = i;
			}
		}
		printf("%d\n",vmax);
	}
	return 0;
}

Computer

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 18
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  We often hear that computer is a magic, a great invention, or even a marvel. But actually computer is just a tool people use everyday. It is a machine that can help people to process many jobs effectively. Moreover, without computer, you can not play ICPC. So, guys, let’s study some stuff about computer here.
  One computer has one CPU (Central Processing Unit). CPU can be idle or processing one job at any time. Jobs come randomly and are stored in the memory until finished. CPU will process jobs according to some strategies. The processing job can be interrupted and saved back so that CPU can be available for other jobs.
  Each job has a release time and a processing time. Assume that we know the schedule of all jobs, please generate a program to minimize the sum of completion times of all jobs using a strategy which assigns and interrupts jobs properly.
  For example, suppose there are two jobs to be completed. Job 1 is released at time 1 and needs 4 time units to process. Job 2’s release time and processing time is 3 and 1. Figures below show three solutions:

  Figure 1 shows a solution with the total complete time 4 + 6 = 10, and the result of Figure 2 and 3 are both 5 + 6 = 11. In fact, Figure 1 shows the optimal solution. 
  Please note that all of the jobs will be released, interrupted and assigned in integer time unit.

Input

  Input may consist of multiple test cases.
  Every test case begins with a line that contains one integer n (1<= n <= 50000) denoting the number of jobs. Each of the following n lines contains 2 integers: ri and pi, (1 <= ri <= 10^9, 1 <= pi <= 10000) denoting the release time and processing time of job i.
  Input is terminated by EOF.

Output

  For every test case, print one line with an integer denoting the minimum sum of completion times.

Sample Input

2
1 4
3 1

Sample Output

10


做的时候发现曹老板很早就一次AC了,所以很早就在做,贪心策略也是对的,但用的暴力贪心,一直超时,最后学长给我看了看代码,用优先队列实现堆.....菜鸡表示又多学了一个新姿势。

注释代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 50010;
struct J{
	ll r,p;
}job[maxn];
priority_queue<ll,vector<ll>,greater<ll> > que;    //堆保存的是完成当前保存任务所需的时间 

bool cmp(const J &a,const J &b){    //将工作按开始时间从小到大排序,若开始时间相同,则按完成需要的时间从小到大排序 
	if(a.r == b.r)
		return a.p<b.p;
	return a.r < b.r;
}

int main(){
	int n,i;
	while(scanf("%d",&n) != EOF){
		for(i=1 ;i<=n ;i++)	
			scanf("%I64d%I64d",&job[i].r,&job[i].p);	
		sort(job+1,job+n+1,cmp);           //排序 
		while(que.size())
			que.pop();                   //清空队列 
		ll time = job[1].r,ans = 0;      //time为当前时间,ans为结果值 
		que.push(job[1].p);              
		for(i=2 ;i<=n ;i++){            //遍历每个工作 
			while(que.size() && time+que.top() <= job[i].r){    //如果存在没有完成的工作且  当前时间加上完成此项工作所需的时间小于下一个工作的开始时间 
				time += que.top();       //完成之前未完成的工作 
				que.pop();
				ans += time;           //保存完成时的时间 
			}
			if(que.size()){
				ll h = que.top();      //拿出所有未完成工作中完成所需时间最短的,记为A 
				que.pop();
				h -= job[i].r - time;   //拿当前时间和下一个工作B开始时间的空隙完成一部分A工作,使A工作完成所需的时间更短 
				que.push(h);            //再把A放进堆 
			}
			que.push(job[i].p);         //把B放进堆 
			time = job[i].r;            //当前时间更新为B工作的开始时间 
		}
		while(que.size()){            //将堆中剩余的工作完成 
			time += que.top();
			que.pop();
			ans += time;
		}
		printf("%I64d\n",ans);
	}
	return 0;
}


1004:

University Rankings

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 40   Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  At present, the university rankings are very popular. They help senior high school students to choose universities for further study. For example, you can find the CHINESE UNIVERSITY RANKINGS at http://www.netbig.com/.
  As we know, a university usually has many different departments, such as department of Computer Science (CS), department of Electronic Engineering (EE), and School of Foreign Languages (FLS). Some of them are quite good when comparing to the other universities, but others are not. So, most of the university rankings are composed of several ranking lists, each list for one department.
  But here comes a problem that sometimes it’s hard to determine which university is better, when comparing two universities with each other. Fortunately, Doctor Bob has advanced a new concept named “absolutely better”, with which the problem above can be partially solved.
  Now, here is an example to explain the concept “absolutely better”:
  Assume that there are three universities (X, Y, Z) and every university has three departments: CS, EE and FLS. And the rankings of different departments are as followed:
  The ranking of CS: X > Y > Z (X>Y means X have a better CS department than Y)
  The ranking of EE: X > Z > Y
  The ranking of FLS: Z > X >Y
  Obviously, each department of University X is better than that of University Y. Then, it’s called that X is absolutely better than Y. Using the “absolutely better” concept, it becomes possible to compare some pairs of the universities.
  Now Bob has the complete rankings of different departments of many universities, and he wants to find k universities (U1,…,Uk) such that Ui is absolutely better that Uj (for any i<j). Could you tell Bob the maximum value of k?

Input

  The first line of the input is a positive integer C. C is the number of test cases
followed.
  The first line of each test case is two positive integer N,M (0<N,M ≤ 100), N is the number of universities and M is the number of departments. And then M lines follow. The i-th (1 ≤ i ≤ M) line contains N numbers Ui(1 ≤ i ≤ N, 1 ≤ Ui ≤ N), indicating the ranking of the i-th department. If University Ui precedes to University Uj in line k, then the k-th department of Ui is better than the k-th department of Uj.

Output

  The output should consist of C lines, one line for each test case. Each line only contains one integer – the maximum value of k as described above. No redundant spaces are needed.

Sample Input

1
3 3
1 2 3
1 3 2
3 1 2

Sample Output

2


这道题赛后问了集训队很崇拜的两位神.,才发现其实是一个排序后求LIS的题,后来一直纠结于如何使用自己平时用来求LIS的一个高效算法来解决这道题,有一个细节始终复杂度很高,最后观摩了一下岑神的代码才发现原来只需要用O(N^2)那个算法就好了,简单又好写....

代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 110;
int n,m;
struct University{
	int a[maxn];
}u[maxn];
int res[maxn];

void init(void){
	int i,j;
	for(i=0 ;i<maxn ;i++){
		res[i] = 1;
		for(j=0 ;j<maxn ;j++){
			u[i].a[j] = 0;
		}
	}
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%d%d",&n,&m);
		int i,j;
		int k;
		for(i=1 ;i<=m ;i++){
			for(j=1 ;j<=n ;j++){
				scanf("%d",&k);
				u[k].a[i] = j;
			}
		}	
		for(i=1 ;i<n ;i++){
			for(j=i+1 ;j<=n ;j++){
				int	k,cnt = 0;
				for(k=1 ;k<=m ;k++){
					if(u[i].a[k] < u[j].a[k])
						cnt++;
				}
				if(cnt == m){
					University tem;
					tem = u[i];
					u[i] = u[j];
					u[j] = tem;
				}
			}
		}
		int ans = 1;
		for(i=1 ;i<=n ;i++){
			for(j=1 ;j<i ;j++){
				int k,cnt = 0;
				for(k=1 ;k<=m ;k++){
					if(u[j].a[k] > u[i].a[k])
						cnt++;
				}
				if(cnt == m && res[j]+1>res[i]){
					res[i] = res[j] + 1;
					if(res[i] > ans)
						ans = res[i];
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


1005:

教材上的一个重积分题....之前翻到过有点印象.....后来WA了几次实在不会写,偷偷翻了翻教材看了公式(比赛好像也能翻纸质资料吧[逃]).....然后过掉了,但内心罪恶感十足.....代码就不好意思贴了qwq

1006:

Gray code

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 95   Accepted Submission(s) : 27
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  Gray code is an interesting code sequence and has many applications in computer science. No matter you have known it before or not, here are some introductions about its features:
  (1) Gray code has 2n unique elements;
  (2) Each element contains n digits of 0 or 1;
  (3) Each pair of adjacent elements has exactly one different digit.
  For example, when n=2, one of the gray code sequence is: 00,01,11,10.
Now, the task is quite simple, given a positive integer n, generate the corresponding Gray code sequence.

Input

  Input may contain multiple test cases. Each test case consists of one positive integer n(n<=16), Input is terminated by a case with n=0, which should not be processed.

Output

  For each test case, output the corresponding Gray code sequence, one element per line. There may be multiple answers, any of them will be accepted. Please output a blank line after each test case.

Sample Input

1
2
0

Sample Output

0
1

00
01
11
10

刚好最近在学数位DP,类似的题遇见过。
两个相邻的n位格雷码,那么他们一定有(n-1)位是相同的,而且那n-1位只是一个更小规模的格雷码。
所以我用了搜索,从小规模的格雷码推到要求规模的格雷码。首先从第一位开始填,第一位要么0,要么1(顺序很重要,之前因为不重视顺序WA了几发)。
然后两位格雷码可以看成:
0 + 0/1
1 + 1/0
共四个,从样例易发现,若前一位是0,则先填0后填1,若前一位是1,则先填1,后填0。
这是我之前的代码,s代表前一位是0还是1。
	if(!s){
		str[len] = '0';
		dfs(str,len+1,0);
		str[len] = '1';
		dfs(str,len+1,1);
	}
	else{
		str[len] = '1';
		dfs(str,len+1,1);
		str[len] = '0';
		dfs(str,len+1,0);
	}
但这样我交了之后还是WA。
后来我自己输出了一下三位格雷码,发现:
0为首位的最后一个三位格雷码是:010
1为首位的第一个三位格雷码是    :111
相邻但有两位都不一样,看来前一位是0,还是1还存在一些差别,然后我把首位为1改成了这样:
	else{
		str[len] = '1';
		dfs(str,len+1,0);
		str[len] = '0';
		dfs(str,len+1,1);
	}
前一位为0时,下一位填1,则s更新为1,但当前一位为1时,为了处理如上例子分界点的特殊情况,我们需要前一位为1时,下一位填1,而s更新为0(好像有点抽象.....)

下面是完整代码:
#include <bits/stdc++.h>
using namespace std;

char str1[25],str2[25];
int n;

void dfs(char* str,int len,bool s){
	if(len == n){
		printf("%s\n",str);
		return;
	}
	if(!s){
		str[len] = '0';
		dfs(str,len+1,0);
		str[len] = '1';
		dfs(str,len+1,1);
	}
	else{
		str[len] = '1';
		dfs(str,len+1,0);
		str[len] = '0';
		dfs(str,len+1,1);
	}
}

int main(){
	while(scanf("%d",&n) != EOF && n){
		str1[0] = '0';
		str2[0] = '1';
		str1[n] = '\0';
		str2[n] = '\0';
		dfs(str1,1,0);
		dfs(str2,1,1);
		printf("\n");	
	} 
	return 0;
}

Hero

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  The beautiful princess is caught by the demon again. Super Mario, her lover, has to take the responsibility to be the hero. The princess is incarcerated in a horrific castle which is smelly and full of monsters. She is very scared. Super Mario is very worried about the princess and wants to see the princess as soon as possible. Though he runs fast, he still wants to know how fast it will take to reach the place where the princess is.
  The castle is a N*M matrix. Initially Mario and princess are located in different positions. Mario can move up, down, left, right, one step per second. Specially, there are some springs distributed in the castle (Don’t ask why, maybe the demon likes to play spring.^_^). These springs are supernatural and may be used as a tool to help Mario if used properly. Each of the spring has an attribute - spring power which is an integer number actually. When Mario enters a grid where there is a spring whose spring power is k, he will be sprung k grids following the direction he enters the grid in no time.



  For example, supposed Mario is in (2,8), and there is a spring in grid (2,7) with 5 spring power. If Mario goes left one step, he will be sprung 5 grids left. So the final position of Mario is (2,2) and the time from (2,8) to (2,2) for Mario is just one second.
  Note that if Mario is sprung outside the matrix, he will be stopped by the wall of the castle and drop on the grid beside the wall. For example, supposed Mario is in (2,8), and there is a spring in grid (2,7) with 10 spring power. If Mario goes left one step, he will just sprung 6 grids left, then stop and drop in grid (2,1).
  Moreover, if the position where Mario will land has a spring, he can be sprung again. That means Mario can be sprung consecutively until he lands on a grid without spring. And only the landing position may affect Mario’s action, all grids that Mario passes by when he is sprung have nothing to do with him. For example, supposed Mario is in (2,8) and there are two grid have springs – (2,7) and (2,5), both are 5 spring power. If Mario steps left, he will be sprung to position (2,2). So the spring in (2,5) does not work on him.
  It is your task to tell Mario the minimum time to reach the princess.

Input

  Input contains several test cases and is terminated by EOF.
  Each case begins with one line containing two integers, n and m (3 <= n,m <= 100), which are the row number and column number of the castle. Then follows a non-negative integer number k, indicating the number of springs. Then follows k lines, each line has three positive integers - x,y, and p, x,y is the coordinate of the spring (2<= x <=n-1,2 <= y <= m-1) and p is the spring power described above. These are followed by 2 lines lastly, the first line contains two integers, which represent the coordinate of Mario, and the other line is the coordinate of the princess.
  You can assume all of the cases are legal. The initial positions of Mario, princess, and springs are distinct. All of the x coordinates are in the range of [1, n] and all of the y coordinates are in the range of [1, m]. And no spring is next to the wall of the castle.

Output

  For each test case, just output one line presenting the minimum time Mario will spend. If Mario can not save the princess (Oh, my god*_* but it may happen), just print “Impossible“ in one line(without quotation marks) .

Sample Input

10 10
0
2 2
7 8
10 10
1
2 7 5
2 8
1 1
10 10
1
2 7 10
2 8
1 1
10 10
2
2 7 5
2 5 5
2 8
1 1
10 10
4
7 9 2
7 7 2
6 8 2
8 8 2
2 2
7 8

Sample Output

11
3
2
3
Impossible


好气啊.....连题都没读的简单迷宫题qwq,但其实真的不难,主要就是处理好走到弹簧的情况,而且存在从一个弹簧跳到另一个弹簧然后继续跳的情况。
所以每个状态可以多加一个变量来表示通过时的方向。
代码:
#include <bits/stdc++.h>
using namespace std;

const int maxn = 105;
const int INF = 1e8;
struct P{
	int x,y;
	int t,status;
	friend bool operator<(P a,P b){
		return a.t > b.t;
	}
};
int n,m,maze[maxn][maxn],vis[maxn][maxn][4];
int sx,sy,gx,gy,tt;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};

bool check(int xx,int yy,int s){
	if(xx<=0 || xx>n || yy<=0 || yy>m || vis[xx][yy][s])
		return false;
	return true;
}

int bfs(void){
	priority_queue<P> que;
	P a,b;
	a.x = sx,a.y = sy,a.t = 0,a.status = -1;
	vis[a.x][a.y][0] = 1;
	vis[a.x][a.y][1] = 1;
	vis[a.x][a.y][2] = 1;
	vis[a.x][a.y][3] = 1;
	que.push(a);
	while(que.size()){
		a = que.top();
		que.pop();
		while(maze[a.x][a.y] > 0){
			if(a.status == 0){
				a.y += maze[a.x][a.y];
				if(a.y > m)
					a.y = m;
			}
			else if(a.status == 1){
				a.y -= maze[a.x][a.y];
				if(a.y <= 0)
					a.y = 1;
			}
			else if(a.status == 2){
				a.x += maze[a.x][a.y];
				if(a.x > n)
					a.x = n;
			}
			else if(a.status == 3){
				a.x -= maze[a.x][a.y];
				if(a.x <= 0)
					a.x = 1;
			}
		}
		if(a.x == gx && a.y == gy){
			return a.t;
		}
		int i;
		for(i=0 ;i<4 ;i++){
			b.x = a.x + dx[i];
			b.y = a.y + dy[i];
			b.t = a.t + 1;
			b.status = i;
			if(check(b.x,b.y,b.status)){
				vis[b.x][b.y][b.status] = 1;
				que.push(b);
			}
		}
	}
	return -1;
}

int main(){
	while(scanf("%d%d",&n,&m) != EOF){
		memset(maze,0,sizeof(maze));
		memset(vis,0,sizeof(vis));
		int k,x,y,p;
		scanf("%d",&k);	
		while(k--){
			scanf("%d%d%d",&x,&y,&p);
			maze[x][y] = p;
		}
		scanf("%d%d",&sx,&sy);
		scanf("%d%d",&gx,&gy);
		int ans = bfs();
		if(ans != -1)
			printf("%d\n",ans);
		else
			printf("Impossible\n");
	}
	return 0;
}

1008:

Reprogramming

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 52   Accepted Submission(s) : 33
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

  Recently Doctor Guan has discovered two new kinds of bacteria and named them BT-U and BT-V .BT-U and BT-V are quite special, they can only live with the help of each other. Doctor Guan did several experiments on BT-U and BT-V, here’re the results:
  Put 100 BT-Us and 80 BT-Vs together , one minute later , there are 20 BT-Us and 80 BT-Vs ,one minute later again , there are 20 BT-Us and 60 BT-Vs, then 20 BT-Us and 40 BT-Vs , then 20 BT-Us and 20 BT-Vs , then these 20 BT-Us and 20 BT-Vs keep alive.
  Put 3 BT-Us and 5 BT-Vs together , one minute later , there are 3 BT-Us and 2 BT-Vs , one more minute later there are 1 BT-U and 2 BT-Vs, then 1 BT-U and 1 BT-V, and this 1 BT-U and 1 BT-V keep alive.
  According to the results above, Doctor Guan has reached a conclusion that when putting x BT-Us and y BT-Vs together, if x=y then they keep alive, if x<y then x BT-Vs would die in one minute, if x>y then y BT-Us would die in one minute.
  Doctor Guan has made a program to determine how many BT-Us and BT-Vs survive when putting x BT-Us and y BT-Vs together. Program is as follow :
#include<fstream.h>
ifstream filein(“reprog.in”);
ofstream fileout(“reprog.out”);
int main(){
  long x,y;
  for(;;){
    filein>>x>>y;
    if(x<=0 || y<=0)break;
    while(x!=y)if(x>y)x-=y;else y-=x;
    fileout<<x<<endl;
  }
  return 0;
}

  
  But this program is quite inefficient; Doctor Guan needs your help to improve it.

Input

  There’re multiple cases . For each case there’s one line containing two integers for x and y. Input is end with two 0’s .

Output

  For each case output the result in a single line.

Sample Input

2 2
1 1
0 0

Sample Output

2
1


简单签到题:
#include <bits/stdc++.h>
using namespace std;

int main(){
	int x,y;
	while(scanf("%d%d",&x,&y) != EOF){
		if(x<=0 && y<=0)
			break;
		while(x!=y){
			if(x>y){
				x %= y;
				if(x == 0)
					x = y;
			}
			else if(x<y){
				y %= x;
				if(y == 0)
					y = x;
			}
		}
		printf("%d\n",x);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值