Codeforces Round #549 (Div. 2) D 数学

https://codeforces.com/contest/1143/problem/D

题意

有nk个城市,第1,k+1,2k+1,...,(n-1)k+1城市有餐厅,你每次能走l距离,a为起始位置离最近餐厅的距离,b为走了一次后离最近餐厅的距离,给出n,k,a,b,求你回到起点最少和最多停留次数

题解

  • \(yl=xnk,有y=xnk/l,即y=lcm(xnk,l)/l\)
  • 枚举a(两种情况),b(两种情况),维护最大,最小值

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,k,a,b,p,d,mx=-1e17,mi=1e17;
ll gcd(ll a,ll b){
    if(b>a)swap(a,b);
    if(b==0)return a;
    else return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}

int main(){
    cin>>n>>k>>a>>b;
    for(ll i=0;i<n;i++){
        p=b+i*k;
        if(p>n*k)p-=k;
        d=abs(p-a);
        mi=min(lcm(n*k,d)/d,mi);
        mx=max(lcm(n*k,d)/d,mx);
    }
    for(ll i=0;i<n;i++){
        p=(k-b)+i*k;
        if(p>n*k)p-=k;
        d=abs(p-a);
        mi=min(lcm(n*k,d)/d,mi);
        mx=max(lcm(n*k,d)/d,mx);
    }
    a=k-a;
    for(ll i=0;i<n;i++){
        p=b+i*k;
        if(p>n*k)p-=k;
        d=abs(p-a);
        mi=min(lcm(n*k,d)/d,mi);
        mx=max(lcm(n*k,d)/d,mx);
    }
    for(ll i=0;i<n;i++){
        p=(k-b)+i*k;
        if(p>n*k)p-=k;
        d=abs(p-a);
        mi=min(lcm(n*k,d)/d,mi);
        mx=max(lcm(n*k,d)/d,mx);
    }
    cout<<mi<<" "<<mx;
}

原文地址:https://www.cnblogs.com/VIrtu0s0/p/10805245.html

时间: 2024-08-30 05:59:33

Codeforces Round #549 (Div. 2) D 数学的相关文章

Codeforces Round #224 (Div. 2) B 数学推理

挺有意思的一道题目,一开始发现了循环节,做了一下,发现许多小地方要补,比较繁琐,做了几个小时的无用功吧,但是循环节肯定可以只是我写搓了,后来又推了公式,发现可以的 首先当b<x的时候,c--,a--那么对于 a,c来说他们之间的差并没有减小,所以真正起到作用的是b>=x的时候,这个时候只有c--,但是答案要求的 是多少次,在b<x的时候 是要经过一定次数的  w-(x - b)来重新使得b>=x,所以第二部分对答案有影响,但是 设方程的话 就不需要多设一个未知数,因为 第一部分肯定

Codeforces Round #549 (Div. 1)

今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The Beatles 这道题就是枚举啦 有两种步长 试一下就好了 如果你的步长是x 那么要跳的次数就是距离除以步长 \[ \frac{n * k * x}{gcd(n * k, x)} \div x = \frac{n * k}{gcd(n * k, x)} \] #include <cmath> #in

Codeforces Round #360 (Div. 2) D 数学推导 E dp

Codeforces Round #360 (Div. 2) A  == B  水,但记一下: 第 n 个长度为偶数的回文数是  n+reverse(n). C    dfs 01染色,水 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i

Codeforces Round #470 (Div 2) B 数学 C 二分+树状数组 D 字典树

Codeforces Round #470 B. Primal Sport 数学题,对 x2 和 x1 分解质因子即可. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b;

A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【数学】

题目: Little C loves number ?3? very much. He loves all things about it. Now he has a positive integer nn. He wants to split nn into 3 positive integers a,b,ca,b,c, such that a+b+c=na+b+c=n and none of the 3 integers is a multiple of 3. Help him to fin

Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y=x^2+bx+c=>y+x^2=bx+c\),转换为点\((x,y+x^2)\)在bx+c的直线上 两个点确定一条抛物线,同时也确定了一条直线 需要选择最上面那些点相邻确定的抛物线,所以维护一个上凸包即可 维护上凸包,当前点在前进方向左边需要向后退,cross(a,b)>=0 代码 #inclu

C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】

题目: Mr. F has nn positive integers, a1,a2,-,an. He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers. But this problem is too simple for him, so he does not want to do it by

Codeforces Round #549 (Div. 2)

A.The Doors 记录最后一个0和1的位置. B.Nirvana 对于每一位,答案有三种情况: 1,取这位原本数字; 2,取x-1,同时让后一位取9; 3,让前面全取9; C.Queen 一个点如果会被删,那么其他的点被删不会影响它最后被删的结果,判断一下那些点会被删, 然后排序. D.The Beatles 显然\( L = c \times k + a - b \) 或 \( L = c \times k - a - b \),对于每个L,\[ step = lcm(n \times

B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)

---恢复内容开始--- Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper. Help Kurt find the maximum possible product of digits among all integers from 1 to n. Input