洛谷 Floating point exception: 8 Floating-point exception. 报错

用g++编译c++程序的时候,出现了报错Floating point exception: 8

后来一经测试,发现rand() % 0搞的鬼,对0取模就会这样,所以用%前一定要判断下非0才行。

是因为使用我的gcd,然后没有对a=0时进行特判

#include <bits/stdc++.h>

using namespace std;
const int N = 50010;
#define int long long
struct Query
{
    int id, l, r;
} q[N];

set<int> st; // 开一个set维护当前区间出现的袜子
int cnt[N], block;
int n, m, a[N], ans[N], ans2[N], sum;

int gcd(int a, int b)
{
    return !b ? a : gcd(b, a % b);
}

int get_block(int x)
{
    return x / block;
}

bool cmp(const Query &x, const Query &y)
{
    int a = get_block(x.l);
    int b = get_block(y.l);
    if (a != b)
        return a < b;
    if (a & 1)
        return x.r < y.r;
    return x.r > y.r;
}

void add(int x, int &res)
{
    // st.insert(x);
    sum += cnt[x];
    cnt[x]++;
}

void del(int x, int &res)
{
    // if (cnt[x] == 0)
    // {
    //     st.erase(x);
    //     return;
    // }
    cnt[x]--;
    sum -= cnt[x];
    // if (cnt[x] == 0)
    //     st.erase(x);
}

signed main()
{
    scanf("%lld%lld", &n, &m);
    block = n/sqrt(m*2/3);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    for (int i = 1; i <= m; i++)
    {
        int l, r;
        scanf("%lld%lld", &l, &r);
        q[i] = {i, l, r};
        ans2[i] = (r - l) * (r - l + 1) / 2;
    }
    
    sort(q + 1, q + 1 + m, cmp);

    for (int k = 1, res = 0, i = 0, j = 1; k <= m; k++)
    {
        int id = q[k].id, l = q[k].l, r = q[k].r;
        
        while (i < r)
            add(a[++i], res);
        while (i > r)
            del(a[i--], res);
        while (j < l)
            del(a[j++], res);
        while (j > l)
            add(a[--j], res);

        // res = 0;
        // for (int it : st)
        //     res += cnt[it] * (cnt[it] - 1) / 2;
        ans[id] = sum;
    }
    for (int i = 1; i <= m; i++)
    {
        if(ans[i] == 0) {cout << "0/1" <<endl;continue;}
        int d = gcd(ans[i], ans2[i]);
        printf("%lld/%lld\n", ans[i]/d, ans2[i]/d);
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

善良的大铁牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值