求助(玄关)

P11362 [NOIP2024] 遗失的赋值

Mobius_CaO @ 2024-12-08 10:55:59

#include<bits/stdc++.h>
#define LL unsigned long long
using namespace std;
const int N=1e6+9;
const int MOD=1e9+7;
LL num[N],n,m,v,qj[N];
bool vis[N];
struct node{
    LL c,d;
};
node sj[N];
bool cmp(node x,node y){
    return x.c<y.c;
}
bool check(){
    for(int i=1;i<m;i++){
        if(sj[i].c==sj[i+1].c&&sj[i].d!=sj[i+1].d)return 1;
    }
    return 0;
}
LL ksm(LL base,LL t){
    LL s=1;
    while(t){
        if(t%2)s*=base;
        t/=2;
        base*=base;
        base%=MOD;
        s%=MOD;
    }
    return s;
}
LL find(LL x){
    if(x==0)return 1;
//  if(x==1)return qj[x]=(v*(v-1)+1)%MOD;//%MOD
//  if(qj[x])return qj[x];
//  else return qj[x]=(ksm(v*v,x-1)*v*(v-1)+v*find(x-1))%MOD;//%MOD%MOD%MOD
    LL s=(ksm(v,x*2))-(ksm(v,x))+(ksm(v,x-1));
    return s%MOD;
}
void solve(){
    LL ans=1;
    ans*=ksm(v,2*(sj[1].c-1));
    ans%=MOD;
    for(int i=2;i<=m;i++){
        ans*=find(sj[i].c-sj[i-1].c);
        ans%=MOD;
    }
    ans*=ksm(v,2*(n-sj[m].c));
    ans%=MOD;
    printf("%lld\n",ans);
}
int main(){
//  freopen("assign.in","r",stdin);
//  freopen("assign.out","w",stdout);
    LL T;
    scanf("%lld",&T);
    while(T--){
        scanf("%lld%lld%lld",&n,&m,&v);
        for(int i=1;i<=m;i++){
            scanf("%lld%lld",&sj[i].c,&sj[i].d);
        }
        sort(sj+1,sj+m+1,cmp);
        if(check()){
            printf("0\n");
            continue ;
        }
        //if(m<=1)solve1();else
        solve();
    }
    return 0;
}
//11:28
/*
1
557584238 1 464289274
309302662 462600170
*/ 
//12:03
//12:35

这是lz的赛时代码60pts

但如果将39行

LL s=(ksm(v,x*2))-(ksm(v,x))+(ksm(v,x-1));

改为

LL s=(ksm(v,x*2))+MOD-(ksm(v,x))+(ksm(v,x-1));

即可AC

蒟蒻不理解这样为什么就对了


by zuohan @ 2024-12-08 16:28:32

+MOD保证不模出负数


|