跳到正文

题解

题解:P16752 [GKS 2020 #B] Bus Routes

ERABY_E_CONNEC_10N约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年6月4日
收录
2026年6月5日
更新
2026年11月9日
篇幅
约 2 分钟
题目
P16752

版本

  1. v1当前

题目传送门

形式化题意

给定一个长度为 NN 的数组 XX,定义序列 a1,a2,...,aNa_1,a_2,...,a_N,满足:

  • aia_i 是 XiX_i 的正整数倍。
  • a1≤a2≤...≤aN≤Da_1\le a_2\le ...\le a_N\le D。

求 a1a_1 的最大值。

思路

直接计算 a1a_1 有些困难,不妨从 aNa_N 向前推算。

因为 aN≤Da_N\le D,且 aNa_N 为 XNX_N 的倍数,所以 aNa_N 的值就为不大于 DD 的 XNX_N 的倍数。即:

aN=⌊DXN⌋×XNa_N=\lfloor \frac{D}{X_N} \rfloor \times X_N

同理,aN−1≤aNa_{N-1}\le a_N,且 aN−1a_{N-1} 为 XN−1X_{N-1} 的倍数,所以 aN−1a_{N-1} 的值就为不大于 aNa_N 的 XNX_N 的倍数。即:

aN−1=⌊XNXN−1⌋×XN−1a_{N-1}=\lfloor \frac{X_N}{X_{N-1}} \rfloor \times X_{N-1}

以此类推,得到答案 a1a_1。

最后注意输出格式,与 DD 的范围 1≤D≤10121\le D\le10^{12}。

CODE:

C++
#include<bits/stdc++.h>
#define int long long//D最大可达10^12
using namespace std;
const int N=1e3+5;//N最大1000
int T;
signed main(){
    cin>>T;
    for(int t=1;t<=T;t++){//便于输出格式的case
        int n,d,x[N],a[N];//含义同题面小写
        cin>>n>>d;
        for(int i=1;i<=n;i++)
            cin>>x[i];
        a[n]=(d/x[n])*x[n];//公式计算a[n]
        for(int i=n-1;i>=1;i--)//从a[n-1]推到a[1]
            a[i]=(a[i+1]/x[i])*x[i];//转移方程
        cout<<"Case #"<<t<<": "<<a[1]<<endl;//输出格式
    }
    return 0;//好习惯
}
原文