首页 > 代码库 > UVALive 7045 Last Defence

UVALive 7045 Last Defence

题意:已知S0和S1,以及技术分享,求S中互不相同的数的个数。

分析:例如S0 = 90,S1 = 17,可得序列90,17, 73,56, 17, 39, 22, 5,  17, 12, 5, 7, 2, 5, 3, 2, 1, 1, 0

很容易发现整个序列先是等差递减数列90, 73, 56, 39, 22, 5-------公差 -17,   数列个数 90 / 17 + 1 = 6

          然后是等差递减数列17, 12, 7, 2----------公差 -5 , 数列个数 17 / 5 + 1 = 4

          然后是等差递减数列5, 3, 1--------公差 -2, 数列个数 5 / 2 + 1 = 3

由此可知,前一个数列的公差的绝对值是后一个数列的第一项,而对于第一个数列,它的最后一项必然是之后某数列的首项

PS:经过多组样例模拟可发现0必存在

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a < b ? b : a
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 2000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int main(){
    int T;
    scanf("%d", &T);
    for(int i = 1; i <= T; ++i){
        llu a, b;
        scanf("%llu%llu", &a, &b);
        printf("Case #%d: ", i);
        if(a < b) swap(a, b);
        if(a == 0 && b == 0){
            printf("1\n");
            continue;
        }
        else if(b == 0){
            printf("2\n");
            continue;
        }
        else if(a == b){
            printf("2\n");
            continue;
        }
        else if(a % b == 0){
            printf("%llu\n", a / b + llu(1));
            continue;
        }
        llu ans = 1;
        llu d = Min(a - b, b);
        ans += a / d;
        while(a % d){
            llu tmp = d;
            d = Min(tmp - a % tmp, a % tmp);
            ans += tmp / d;
            a = tmp;
        }
        printf("%llu\n", ans);
    }
    return 0;
}

 

UVALive 7045 Last Defence