可莉学除法 出自HRBUST

本文详细解读了一个C++程序,使用map数据结构解决计数问题,针对输入值x的不同情况,计算特定规则下的结果。程序逻辑清晰,展示了在主函数中处理多个测试用例的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上篇 上一篇代码是水过去的 现在这个代码没有问题

#include <bits/stdc++.h>
#define endl '\n'
#define buff ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
/*
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize(3)
*/
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpll;
const ll MAX_INT = 0x3f3f3f3f;
const ll MAX_LL = 0x3f3f3f3f3f3f3f3f;
const ll CF = 2e5 + 9;
const ll mod = 1e9 + 7;
map<ll, ll> q;

void solve()
{
    q.clear();
    ll n, x;
    ll ans = 0;
    cin >> n >> x;
    for (int i = 0; i < n; i++)
    {
        ll a;
        cin >> a;
        q[a] += 1;
    }
    if (x != 0 && x != 1)
    {
        for (map<ll, ll>::iterator it = q.begin(); it != q.end(); it++)
        {
            if (q.count((it->first) * x) && (it->first) != 0)
            {
                ans += q[(it->first) * x] * (it->second);
            }
        }
    }
    else if (x == 0)
    {
        ll all = 0;
        for (map<ll, ll>::iterator it = q.begin(); it != q.end(); it++)
        {
            all += it->second;
        }
        ans = q[0] * (all - q[0]);
    }
    else if (x == 1)
    {
        for (map<ll, ll>::iterator it = q.begin(); it != q.end(); it++)
        {
            if ((it->first) != 0)
                ans += ((it->second) - 1) * (it->second);
        }
    }
    cout << ans << "\n";
}

int main()
{
    buff;
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值