【找规律】UVALive

来源:互联网 发布:禄宏微交易软件下载 编辑:程序博客网 时间:2024/06/01 10:02

Problem Description

已知S0, S1。Si = |si-1 - si-2| for i>=2。s[]集合有多少个不同的元素

思路:

找规律,模拟演算过程。例如a = S0 = 10, b = s1 = 3;
s2 = 7, s3 = 4, s4 = 3, s5 = 1。先看这里10 7 4 1分别是10 - 3t的到的结果,能减多少次10/3 = 3。这3代表10 7 4这三个数。
接下来相当于变成s0 = b = 3, s1 = a%b = 1 重新循环上述操作

#include<bits/stdc++.h>using namespace std;#define ll long longll solve(ll a, ll b){    ll ans = 0;    if(!b) return 1;//0也算一个不同的数    else    {        ans += a/b + solve(b, a%b);//不断循环,求a/b        return ans;    }    return ans;}int main(){    int T, Case = 1;    ll a, b;    scanf("%d", &T);    while(T--)    {        scanf("%lld %lld", &a, &b);        if(b > a) swap(a, b);        if(!b && a) printf("Case #%d: 2\n", Case++);        else printf("Case #%d: %lld\n", Case++, solve(a, b));    }    return 0;}
原创粉丝点击