Last Defence
时间限制:1000 ms | 内存限制:65535 KB
描述
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 .
输入
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 ≤ 10^18).
输出
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 .
样例输入
2
7 4
3 5
样例输出
Case #1: 6
Case #2: 5
来源
Yougth
我的思路:首先这道题run time error了。。任何两个数,进行题中的运算,得出是一组数,最后肯定是x, x,0,x,x,0,x,x,0……循环的,所以当遇到x,x,0时候就停止,然后把前面的数和x,0进行处理,计算一下一共有多少不同的数。。希望大家帮看一下,这个代码运行没有错误,结果也对。
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
int a[];
int main()
{
int T,i,j;
int g=;
long long A,B;
cin>>T;
while(T--)
{
scanf("%lld%lld",&A,&B);
a[]=A;
a[]=B;
i=;
while()
{
a[i]=fabs(a[i-]-a[i-]);
a[i+]=fabs(a[i]-a[i-]);
a[i+]=fabs(a[i+]-a[i]);
if(a[i]==a[i+]&&a[i+]==)
{ a[i+]=;
break;
}
i++;
}
int n=i+;
sort(a,a+i+);
for(i=;i<n-;i++)
{
if(a[i]==a[i+])
{
for(j=i;j<n-;j++)
a[j]=a[j+];
i--;
n--;
}
}
printf("Case #%d: %d\n",g,n);
g=g+;
}
return ;
}