将映射传递给函数C ++时发现分段错误

时间:2019-05-10 12:41:43

标签: c++ dictionary stl segmentation-fault

我找不到在calculate_grundy中调用main的行上发生的分段错误。请帮忙。

在调试代码时,我还添加了调试器的屏幕截图。

这是一场竞赛的问题,所以请不要回答逻辑问题,而要帮助我找到细分错误。

我正在传递dp映射来存储子问题,就像在动态编程中一样。

#include<bits/stdc++.h>
#define ll long long int
using namespace std;

ll calculate_mex(unordered_set<ll> Set)
{
    ll mex = 0;
    while(Set.find(mex)!=Set.end())
    {
        mex++;
    }
    return mex;
}

ll calculate_grundy(ll n,ll m,map<pair<ll,ll>,ll> &dp)
{
    auto it = dp.find(make_pair(n,m));
    if(it!=dp.end())
    {
        return it->second;
    }
    else
    {
        ll ans;
        ll greater = max(n,m);
        ll smaller = min(n,m);
        if(n==m || m==0 || n==0)
        {
            return 0;
        }
        else if(n==1 || m==1)
        {
            return n==1?m:n;
        }
        unordered_set<ll> Set;
        ll limit = greater%smaller;
        for(ll i=1;i<=limit;i++)
        {
            ll mult = smaller*i;
            Set.insert(calculate_grundy(greater-mult,smaller,dp));
        }
        ans = calculate_mex(Set);
        dp.insert(make_pair(make_pair(n,m),ans));
        return ans;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m;
        cin>>n>>m;
        map<pair<ll,ll>,ll> dp;
        if(calculate_grundy(n,m,dp)==0)
        {
            cout<<"Ari"<<"\n";
        }
        else
        {
            cout<<"Rich"<<"\n";
        }
    }
}

image

0 个答案:

没有答案