【NOI OL #2】涂色游戏

题目链接

首先设$a>b$,$a$红$b$蓝,那么对于$[1,operatorname{lcm}(a,b)]$这个区间内来说,涂红色的格子数就为

$$dfrac{operatorname{lcm}(a,b)}{a}=dfrac{a imes b}{gcd(a,b)} imes dfrac{1}{a}=dfrac{b}{gcd(a,b)}$$

同理,涂蓝色的格子数就为$dfrac{a}{gcd(a,b}$。

但是在$0$和$operatorname{lcm}(a,b)$处是应该填红色的,所以蓝色多算了一个。

那我们要求红色分隔开的段数,那就是$x=dfrac{b}{gcd(a,b)}$,然后其中均匀分散着$y=dfrac{a}{gcd(a,b)}-1$个蓝色格子。

所以我们最多连续的蓝色格子数目就是$lceildfrac{y}{x} ceil$。

代码(100分):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define IL inline
#define RG register
#define _1 first
#define _2 second
using namespace std;

    int t,a,b,k;
    
IL int gcd(int a,int b){
    int r=a%b;
    while(r){
        a=b;    b=r;    r=a%b;
    }
    return b;
    
}
    
IL void sol(){
    scanf("%d%d%d",&a,&b,&k);
    
    if(k==1){
        printf("NO
");    return ;
    }
    if(a==b){
        printf("YES
");return ;
    }
    
    if(a<b)    swap(a,b);
    int g=gcd(a,b);
    a/=g;    b/=g;
    if((a-1)/b+((a-1)%b!=0)<k)
        printf("YES
");
    else 
        printf("NO
");
    
}

int main(){
    scanf("%d",&t);
    while(t--)
        sol();

    return 0;

}
View Code
原文地址:https://www.cnblogs.com/Hansue/p/12991065.html