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;
}
相关推荐
渡我白衣5 小时前
计算机组成原理(14):算术逻辑单元ALU
大数据·人工智能·算法·机器学习·计组·数电·alu
柳鲲鹏5 小时前
OpenCV视频实时跟踪目标,多种算法,python版
opencv·算法·音视频
朱峥嵘(朱髯)5 小时前
数据库如何根据估计 NDV,以及通过分区 NDV 推导全局 NDV
数据库·算法
山上三树5 小时前
详细介绍 C 语言 typedef 及与 #define 的核心对比
c语言·数据结构·算法
释怀°Believe5 小时前
Daily算法刷题【面试经典150题-7️⃣位运算/数学/】
算法·面试·职场和发展
2401_876221345 小时前
因数个数、因数和、因数积
c++·算法
云里雾里!5 小时前
LeetCode 744. 寻找比目标字母大的最小字母 | 从低效到最优的二分解法优化
算法·leetcode
一条大祥脚5 小时前
26.1.3 快速幂+容斥 树上dp+快速幂 带前缀和的快速幂 正序转倒序 子序列自动机 线段树维护滑窗
数据结构·算法
二狗哈5 小时前
czsc入门5: Tick RawBar(原始k线) NewBar (新K线)
算法·czsc
꧁Q༒ོγ꧂5 小时前
算法详解(四)--排序与离散化
数据结构·算法·排序算法