Codeforces Round 957 (Div. 3) F. Valuable Cards

题目

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
#define ll long long

const int maxn = 1e6 + 5, inf = 1e18, maxm = 4e4 + 5, base = 37;
const int N = 1e4;
// const int mod = 1e9 + 7;
// const int mod = 998244353;
const __int128 mod = 212370440130137957LL;

int n, m;
int a[maxn], b[maxn];
//long long ? maxn ? n? m?


void solve(){
    ll res = 0;
    int x;
    cin >> n >> x;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    vector<int> divs;
    divs.pb(0);
    vector<int> id(x + 1, 0);
    for(int i = 1; i * i <= x; i++){
        if(x % i == 0){
            divs.pb(i);
            // id[i] = divs.size() - 1;
            if(x / i != i){
                divs.pb(x / i);
                // id[x / i] = divs.size() - 1;
            }
        }
    }
    int sz = divs.size() - 1;
    sort(divs.begin() + 1, divs.begin() + sz + 1);//排序因为后面要从大的因数开始更新
    for(int i = 1; i <= sz; i++){
        id[divs[i]] = i;//x的因数到编号的映射
    }
    vector<int> can(sz + 1, 0);//can[i]表示因数divs[i]能不能被凑出
    can[1] = 1;
    res = 1;
    for(int i = 1; i <= n; i++){
        if(x % a[i] != 0) continue;//不是x的因数,直接忽略
        if(can[id[x / a[i]]]){
            res++;
            can.assign(sz + 1, 0);
            can[1] = 1;
            can[id[a[i]]] = 1;
            continue;
        }
        //必须从大的因数开始更新can,
        //设
        //下标 : 1 2 3 4
        //divs : 1 2 4 8
        //can  : 1 0 0 0
        //当前数a[i] : 2
        //若从小的因数开始更新,那么更新之后
        //can  : 1 1 1 1 (显然错误)
        for(int j = sz; j >= 1; j--){
            if(divs[j] % a[i] == 0 && can[id[divs[j] / a[i]]]){
                can[j] = 1;
            }
        }
    }
    cout << res << '\n';
}
    
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cout << fixed << setprecision(9);

    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}
相关推荐
吞下星星的少年·-·1 小时前
U549625 小苯的区间和疑惑(枚举,前后缀)
算法
李剑一1 小时前
到底啥是千年虫2.0?为什么量子计算能够突破公钥私钥体系?什么是后量子密码?
算法·ai编程
Techblog of HaoWANG2 小时前
具身智能机器人专题(7)大模型 / 语音 / RAG 的容器化架构与系统开发
人工智能·算法·大模型·边缘计算·具身智能
专注仿真2 小时前
仿真智能体训练(评估指标篇):探测/杀伤链时延算法详解与实战
算法·afsim智能体·afsim智能体训练·评估算法·探测时延算法·杀伤链时延算法
r_oo_ki_e_3 小时前
牛牛与切割机
java·算法
小糖学代码3 小时前
LLM系列:4.transformer算法:4.NLTK(补充)
深度学习·算法·transformer
geovindu5 小时前
python: PCA
开发语言·后端·python·算法
凯程序猿1号6 小时前
基于射线法与球面外积的WGS84电子围栏点在多边形内判定及边界容差算法
算法·电脑
budingxiaomoli6 小时前
动态规划---子序列
算法·动态规划
释厄6236 小时前
尺子必回量与劣币生良币——可证伪明的尺子性·同源可转化
大数据·算法·开源