QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#874076#9682. nim 游戏thomaswmyCompile Error//C++206.1kb2025-01-27 14:37:222025-01-27 14:37:23

Judging History

你现在查看的是最新测评结果

  • [2025-01-27 16:14:54]
  • hack成功,自动添加数据
  • (/hack/1492)
  • [2025-01-27 14:37:23]
  • 评测
  • [2025-01-27 14:37:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int M=2e4+10;
const int P=133331;
typedef long long ll;
const ll Mod=1e18+3;
const ll inf=4e18;

int T;
int n,m;
ll A[N];
ll a[N],b[N];
vector<pair<ll,int> > aa[61];
int cntt[61];
ll mnc;

struct Node{
    vector<pair<int,ll> >  arr;

    void clear() {
        arr.clear();
    }

    void ins(int x,ll y) {
        arr.push_back({x,y});
    }

    void upd() {
        sort(arr.begin(),arr.end());
        static ll add[N];
        for(auto i:arr) add[i.first]=0;
        for(auto i:arr) add[i.first]+=i.second;
        vector<pair<int,ll> > res;
        for(auto i:arr) {
            if(!add[i.first]) continue;
            res.push_back({i.first,add[i.first]});
            add[i.first]=0;
        }
        swap(arr,res);
    }

    ll geth() {
        ll res=0;
        for(auto i:arr) res=((__int128)res*P+i.first)%Mod;
        for(auto i:arr) res=((__int128)res*P+(__int128)i.first*i.second)%Mod;
        return res;
    }

    void print() {
        printf("%d\n",arr.size());
        for(auto i:arr) printf("%d ",i.first);printf("\n");
        for(auto i:arr) printf("%lld ",i.second);printf("\n");
    }

    bool chk(ll xr) {
        ll sum=0;
        for(auto i:arr) xr^=A[i.first],xr^=A[i.first]+i.second,sum+=i.second;
        return !xr && sum==mnc;
    }
};

vector<Node> ans;
unordered_set<ll> st;
vector<int> valid;

void init() {
    ans.clear(),st.clear(),valid.clear();
    ans.reserve(m),valid.reserve(n);
    for(ll i=0;i<=60;i++) {
        cntt[i]=0;
        aa[i].clear();
        for(int j=1;j<=n;j++) {
            cntt[i]+=a[j]>>i&1ll;
            if(a[j]>>i&1ll) continue;
            ll val=a[j]&((1ll<<i)-1);
            aa[i].push_back({val,j});
        }
        sort(aa[i].begin(),aa[i].end());
        reverse(aa[i].begin(),aa[i].end());
    }
}

ll calc(ll *a,ll now,ll xr,vector<int> &arr) {
    if(now<0) return 0;
    if(!(xr>>now&1ll)) return calc(a,now-1,xr,arr);
    ll ans=0;
    pair<ll,int> mx={-1,0};
    for(auto j:aa[now]) {
        if(j.first!=(a[j.second]&((1ll<<now)-1))) continue;
        mx=j;
        break;
    }
    for(int i:arr) {
        ll val=a[i]&((1ll<<now)-1);
        mx=max(mx,{val,i});
        break;
    }
    if(!mx.second) {
        if(a==b) {
            return inf;
        }
        for(int j=1;j<=n;j++) b[j]=a[j];
        ll mn=inf;
        for(int j=1;j<=n;j++) {
            int k=now+1;
            while(k<60 && ((cntt[k]==n-1 && !(a[j]>>k&1)) || (a[j]>>k&1))) k++;
            ll cnt=(1ll<<k)-(a[j]&((1ll<<k)-1));
            b[j]+=cnt;
            arr.push_back(j);
            ll res=calc(b,60,xr^b[j]^(b[j]-cnt),arr)+cnt;
            if(res<mn) {
                mn=res;
                valid.clear();
            }
            if(mn==res) valid.push_back(j);
            arr.pop_back();
            b[j]-=cnt;
        }
        return mn;
    }
    ll cnt=(1ll<<now)-mx.first;
    arr.push_back({mx.second});
    ans+=cnt,a[mx.second]+=cnt;
    ans+=calc(a,now-1,xr^a[mx.second]^(a[mx.second]-cnt),arr);
    a[mx.second]-=cnt;
    return ans;
}

void solve(ll *a,ll now,ll xr,vector<int> arr,Node nd,ll sum) {
    sort(arr.begin(),arr.end()),arr.erase(unique(arr.begin(),arr.end()),arr.end());
    if(ans.size()>=m) return ;
    if(now<0) {
        nd.upd();
        ll h=nd.geth();
        if(st.find(h)!=st.end()) return ;
        st.insert(h);
        ans.push_back(nd);
        return ;
    }
    if(!(xr>>now&1ll)) return solve(a,now-1,xr,arr,nd,sum);
    pair<ll,int> mx={-1,0};
    for(auto j:aa[now]) {
        if(j.first!=(a[j.second]&((1ll<<now)-1))) continue;
        mx=j;
        break;
    }
    for(int i:arr) {
        if(a[i]>>now&1ll) continue;
        ll val=a[i]&((1ll<<now)-1);
        mx=max(mx,{val,i});
    }
    if(!mx.second) {
        if(a==b) return ;
        for(int j=1;j<=n;j++) b[j]=a[j];
        for(int j:valid) {
            int k=now+1;
            while(k<60 && ((cntt[k]==n-1 && !(a[j]>>k&1)) || (a[j]>>k&1))) k++;
            ll cnt=(1ll<<k)-(a[j]&((1ll<<k)-1));
            b[j]+=cnt;
            arr.push_back(j);
            Node v=nd;
            v.ins(j,cnt);
            solve(b,60,xr^b[j]^(b[j]-cnt),arr,v,sum+cnt);
            if(ans.size()>=m) return ;
            arr.pop_back();
            b[j]-=cnt;
        }
        return ;
    }
    ll lst=inf;
    for(auto j:aa[now]) {
        if(j.first!=(a[j.second]&((1ll<<now)-1))) continue;
        ll cnt=(1ll<<now)-(a[j.second]&((1ll<<now)-1));
        Node v=nd;
        arr.push_back({j.second});
        v.ins(j.second,cnt);
        a[j.second]+=cnt;
        bool flag=0;
        if(cnt==lst) flag=1;
        else flag=calc(a,now-1,xr^a[j.second]^(a[j.second]-cnt),arr)+sum+cnt==mnc;
        lst=cnt;
        if(flag) solve(a,now-1,xr^a[j.second]^(a[j.second]-cnt),arr,v,sum+cnt);
        if(ans.size()>=m) return ;
        arr.pop_back();
        a[j.second]-=cnt;
        if(!flag) break;
    }
    lst=inf;
    for(int j:arr) {
        ll cnt=(1ll<<now)-(a[j]&((1ll<<now)-1));
        Node v=nd;
        arr.push_back({j});
        v.ins(j,cnt);
        a[j]+=cnt;
        bool flag=0;
        if(cnt==lst) flag=1;
        else flag=calc(a,now-1,xr^a[j]^(a[j]-cnt),arr)+sum+cnt==mnc;
        lst=cnt;
        if(flag) solve(a,now-1,xr^a[j]^(a[j]-cnt),arr,v,sum+cnt);
        if(ans.size()>=m) return ;
        arr.pop_back();
        a[j]-=cnt;
        if(!flag) break;
    }
    return ;
}

int main() {
    // freopen("nim.in","r",stdin);
    // freopen("nim.out","w",stdout);
    scanf("%*d%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        ll xr=0;
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]),A[i]=a[i],xr^=a[i];
        init();
        mnc=calc(a,60,xr,{});
        printf("%lld\n",mnc);
        Node nd;
        nd.clear();
        solve(a,60,xr,{},nd,0);
        printf("%lld\n",ans.size());
        // assert(ans.size()<=m);
        for(Node i:ans) {
            // if(!i.chk(xr)) printf("???????????????\n"),assert(0);
            i.print();
        }
    }
    return 0;
}

详细

answer.code: In member function ‘void Node::print()’:
answer.code:51:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘std::vector<std::pair<int, long long int> >::size_type’ {aka ‘long unsigned int’} [-Wformat=]
   51 |         printf("%d\n",arr.size());
      |                 ~^    ~~~~~~~~~~
      |                  |            |
      |                  int          std::vector<std::pair<int, long long int> >::size_type {aka long unsigned int}
      |                 %ld
answer.code: In function ‘int main()’:
answer.code:218:17: error: cannot bind non-const lvalue reference of type ‘std::vector<int>&’ to an rvalue of type ‘std::vector<int>’
  218 |         mnc=calc(a,60,xr,{});
      |             ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from answer.code:1:
/usr/include/c++/14/bits/stl_vector.h:531:7: note:   after user-defined conversion: ‘constexpr std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::allocator<int>]’
  531 |       vector() = default;
      |       ^~~~~~
answer.code:84:41: note:   initializing argument 4 of ‘ll calc(ll*, ll, ll, std::vector<int>&)’
   84 | ll calc(ll *a,ll now,ll xr,vector<int> &arr) {
      |                            ~~~~~~~~~~~~~^~~
answer.code:223:20: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘std::vector<Node>::size_type’ {aka ‘long unsigned int’} [-Wformat=]
  223 |         printf("%lld\n",ans.size());
      |                 ~~~^    ~~~~~~~~~~
      |                    |            |
      |                    |            std::vector<Node>::size_type {aka long unsigned int}
      |                    long long int
      |                 %ld
answer.code:212:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  212 |     scanf("%*d%d",&T);
      |     ~~~~~^~~~~~~~~~~~
answer.code:214:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  214 |         scanf("%d%d",&n,&m);
      |         ~~~~~^~~~~~~~~~~~~~
answer.code:216:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  216 |         for(int i=1;i<=n;i++) scanf("%lld",&a[i]),A[i]=a[i],xr^=a[i];
      |                               ~~~~~^~~~~~~~~~~~~~