Last Defense 西安现场赛 欧几里得出发的应用

  这道题的意思是给你两个数A, B, s0 = A, s1 = B, si = |si-1 - si-2|, 让你求出里面有几个不同的数, 首先我们可以确定对于数y和x,y一定能写成kx+b的形式,在数列的生成过程中,会出现kx+b、x、(k-1)x+b、(k-2)x+b、x、...、2x+b、x、x+b、b、x,其中出现的不同数字个数就是(kx+b)/ x,之后问题变成了数x和b的问题,最后可以发现这就是一个辗转相除法的过程。每做一次辗转相除gcd(x,y),不同数字个数就多了x/ y。最后加上一个末尾出现的0。

还有一些特殊情况需要考虑,比如有数字是0。。 代码如下:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
LL a, b;

int main()
{
    int T;
    cin>>T;
    int kase = 0;
    while(T--)
    {
        cin>>a>>b;
        if(a==0 && b==0)
        {
            printf("Case #%d: 1\n", ++kase);
            continue;
        }
        else if(a==0 || b==0)
        {
            printf("Case #%d: 2\n", ++kase);
            continue;
        }
        LL res = 0;
        while(1)
        {
            if(a < b) swap(a, b);
            if(b == 0) break;
            res += a/b;
            LL tp = b;
            b = a%b;
            a = tp;
        }
        printf("Case #%d: %I64d\n", ++kase, res+1);
    }
    return 0;
}
时间: 2024-08-25 15:46:16

Last Defense 西安现场赛 欧几里得出发的应用的相关文章

2014西安现场赛F题 UVALA 7040

地址 题意:求在m种颜色中挑选k种颜色,给n个花朵涂色有几种方法. 分析:画图可以发现,基本的公式就是k ×(k-1)^(n-1).但这仅保证了相邻颜色不同,总颜色数不超过k种,并没有保证恰好出现k种颜色:接着就是一个容斥问题,上述计算方法中包含了只含有2.3.….(k-1)种颜色的情况,需要通过容斥原理去除.假设出现p (2 <= p <= k-1)种颜色,从k种颜色中选取p种进行涂色,方案数为C(k,p) × p × (p-1)^(n-1) :总的方案数就是C(m,k) × ( k × (

CF GYM 100548 Built with Qinghuai and Ari Factor(2014ACM西安现场赛Problem A)

ProblemA. Built with Qinghuai and Ari Factor Description DISCLAIMER: Allnames, incidents, characters and places appearing in this problem arefictitious. Any resemblance to actual events or locales or realpersons, living or dead, is purely coincidenta

CF GYM 100548 Last Defence(2014ACM西安现场赛Problem K)

ProblemK. Last Defence Description Given two integersA and B. Sequence S is defined as follow: ? S0 = A ? S1 = B ? Si = |Si?1 ?Si?2| for i ≥ 2 Count the number ofdistinct numbers in S. Input The first line ofthe input gives the number of test cases,

CF GYM 100548 Color(2014ACM西安现场赛Problem F)

ProblemF. Color Description Recently, Mr. Bigrecieved n flowers from his fans. He wants to recolor those flowerswith m colors. The flowers are put in a line. It is not allowed tocolor any adjacent flowers with the same color. Flowers i and i + 1are s

CF GYM 100548 The Problem Needs 3D Arrays(2014ACM西安现场赛Problem C)

ProblemC. The Problem Needs 3D Arrays Description A permutation is asequence of integers p1, p2, . . . , pn,consisting of n distinct positive integers and each of them does notexceed n. Assume that r(S) of sequence S denotes the number ofinversions i

CF GYM 100548 The Problem to Make You Happy(2014ACM西安现场赛Problem H)

ProblemH. The Problem to Make You Happy Description Alice and Bob aregood friends, as in every other storyline. One day Alice and Bob areplaying an interesting game. The game is played on a directedgraph with n vertices and m edges, Alice and Bob hav

codeforces 100548F (西安现场赛F题):容斥原理

题目大意: 对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数 分析: 从m种颜色中选出k种,有c(m,k)种方法,那么我们只用考虑 k种颜色的涂法即可 显然第一个物品有k种涂法,后面的因为不能跟前面的相同都只有k-1种涂法 因此容易想到一个公式:k*(k-1)^(n-1) 但是这个公式算的是 不超过k种颜色的涂法,题目要求必须k种,怎么办呢? 先考虑一个简化版的问题: 用而且用完5种颜色涂不相关的五个物品的方案数 用阶乘的方法可以算出

第39届ACM国际大学生程序设计竞赛 亚洲区域赛(现场赛)西安站

 第39届ACM国际大学生程序设计竞赛 亚洲区域赛(现场赛)西安赛区总结报告 报告人:田思明 队名:ACpioneer 队长:陈志阳,队员:顾振兴,田思明 西安区域赛告下帷幕,我和陈志阳,顾振兴组成的ACpioneer队最终获得了一块宝贵的铜牌.首先要感谢陈志阳和顾振兴两位杰出队友的努力训练和出色表现,我作为一个新人跟着他们学到了很多很多,也十分珍惜和他们在一起的训练,比赛时光,其次要感谢陈志老师,不辞辛劳陪我们5队和6队前往西安参加比赛,还要感谢集训队所有曾经帮过我们的所有队员们,记得cdy

hdu 4779 Tower Defense 2013杭州现场赛

1 /** 2 题意: 有两种塔,重塔,轻塔.每种塔,能攻击他所在的一行和他所在的一列, 轻塔不 能被攻击,而重塔可以被至多一个塔攻击,也就是说重塔只能被重塔攻击.在一个n*m 的矩阵中,最少放一个塔,可放多个 3 问,给定p个重塔,q个轻塔,问有多少种放法.. 4 5 思路: 1. 一行中有两个重塔, 6 2. 一列中有两个重塔 7 3. 在该行及在该行塔所在的列只有一个塔,重塔或者轻塔. 8 对以上三种情况 9 挨个处理: 10 1. 设有i行有两个重塔,j列有两个重塔,则一共占 i+2*j