【打CF,学算法——三星级】CF Gym 100548K Last Denfence

原创 2015年07月07日 23:39:57

【CF简介】

提交链接:Last Denfence


题面:

 Last Defence
Description
    Given two integers A and B. Sequence S is defined as follow:
    • S0 = A
    • S1 = B
    • Si = |Si−1 − Si−2| for i ≥ 2
    Count the number of distinct numbers in S.
Input
    The first line of the input gives the number of test cases, T. T test cases follow. T is about
    100000.
    Each test case consists of one line - two space-separated integers A, B. (0 ≤ A, B ≤ 1018).
Output
    For each test case, output one line containing “Case #x: y”, where x is the test case
    number (starting from 1) and y is the number of distinct numbers in S.
Samples
Sample Input 

   2
  7 4
  3 5

Sample Output
Case #1: 6
Case #2: 5


解题:

    过程便是辗转相减法,然而模拟这个过程会超时,改进为辗转相除法。直接拿大的数除小的数,即需要减的次数,加到结果中,最后结果便是答案。


代码:

#include <iostream>
using namespace std;
int main()
{
	long long int a,b,n,ans,tmp;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
	cout<<"Case #"<<i<<": ";
	  ans=0;
      cin>>a>>b;    
	  if(a==0&&b==0)
		 {
			 cout<<"1\n";
			 continue;
		 }
	   if(a==0||b==0)
		  {
			  cout<<"2\n";
			  continue;
		  }
	  while(b)
	  {
		  ans+=a/b;
		  tmp=b;
		  b=a%b;
		  a=tmp;
	  }
      cout<<ans+1<<endl;
	}
	return 0;
}

版权声明:欢迎交流!

文章标题 UVALive 7045:Last Defence(辗转相除思想)

Last DefenceGiven two integers A and B. Sequence S is defined as follow: • S0 = A • S1 = B • Si = |Si...

ACM 2015西安赛区题目

Problem F. October 26, 2014 Color Description Recently, Mr. Big recieved n flowers from his fans...

The 2014 ACM-ICPC Asia Regional Contest Xi'an Site K ast Defence

ProblemK. Last Defence Description Given two integersA and B. Sequence S is defined as follow: ...

【打CF,学算法——一星级】Gym 100548A Built with Qinghuai and Ari Factor (Asia Xian)

题目链接:Built with Qinghuai and Ari Factor 题面: Built with Qinghuai and Ari Factor Description DISCLAI...

【打CF,学算法——三星级】Codeforces 9C Hexadecimal's Numbers (解法汇总)

题目链接:http://codeforces.com/problemset/problem/9/C 题面:

【打CF,学算法——三星级】CodeForces 689B Mike and Shortcuts (最短路+spfa)

题目链接:CF 689B 题面: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test ...

【打CF,学算法——三星级】CodeForces 615B Longtail Hedgehog (DFS/拓扑排序)

题目链接:CF 615B 题面: B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 2...

【打CF,学算法——三星级】CodeForces 645C Enduring Exodus (二分+贪心)

【CF简介】 提交链接:CF 645C 题面: C. Enduring Exodus time limit per test 2 seconds memor...

【打CF,学算法——三星级】CodeForces 216B Forming Teams (图论)

【CF简介】 提交链接:CF 216B 题面: B. Forming Teams time limit per test 2 seconds memor...

【打CF,学算法——三星级】CodeForces 567D One-Dimensional Battle Ships (二分)

题目链接:CF 567D 题面: D. One-Dimensional Battle Ships time limit per test 1 second memory limit per...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【打CF,学算法——三星级】CF Gym 100548K Last Denfence
举报原因:
原因补充:

(最多只允许输入30个字)