zoukankan      html  css  js  c++  java
  • 【Luogu】P2220容易题(快速幂)

      这题真是“容易”。呵呵呵。

      参考题解:xyz32768

      代码

      

    #include<cstdio>
    #include<map>
    #include<algorithm>
    #include<cctype>
    #define mod 1000000007
    using namespace std;
    map<long long,bool> vis;
    inline long long read(){
        long long num=0,f=1;
        char ch=getchar();
        while(!isdigit(ch)){
            if(ch=='-')    f=-1;
            ch=getchar();
        }
        while(isdigit(ch)){
            num=num*10+ch-'0';
            ch=getchar();
        }
        return num*f;
    }
    
    inline long long mul(long long a,long long b){
        long long ret=0;
        if(b==1)    return a;
        while(b){
            if(b&1)    ret=(ret+a)%mod;
            a=(a+a)%mod;
            b>>=1;
        }
        return ret;
    }
    
    inline long long Pow(long long a,long long b){
        long long ret=1;
        if(b==1)    return a;
        while(b){
            if(b&1)    ret=mul(ret,a);
            a=mul(a,a);
            b>>=1;
        }
        return ret;
    }
    
    long long s[1000010];
    
    struct Line{
        long long x,y;
        bool operator <(const Line &a)const{
            if(x!=a.x)    return x<a.x;
            return y<a.y;
        }
    }w[1000010];
    long long tot;
    int main(){
        freopen("in.txt","r",stdin);
        freopen("out1.txt","w",stdout);
        long long n=read(),m=read(),q=read();
        long long sum;
        if(n&1)    sum=mul((1+n)>>1,n);
        else    sum=mul(1+n,n>>1);
        for(long long i=1;i<=q;++i)    s[i]=sum;
        for(long long i=1;i<=q;++i)    w[i]=(Line){read(),read()};
        sort(w+1,w+q+1);
        for(long long i=1;i<=q;++i){
            long long x=w[i].x;
            if(!vis[x]){
                vis[x]=1;
                tot++;
            }
        }
        long long ans=Pow(sum,m-tot);
        for(long long i=1;i<=q;++i){
            long long d=sum;
            long long j;
            d-=w[i].y;
            for(j=i+1;w[j].x==w[j-1].x;j++){
                if(w[j].y==w[j-1].y)    continue;
                d-=w[j].y;
            }
            d=(d%mod+mod)%mod;
            ans=(ans*d)%mod;
            i=j-1;
        }
        printf("%lld",ans);
        return 0;
    }
  • 相关阅读:
    [hdu4631 Sad Love Story]最近点对,枚举
    [hdu4629 Burning]三角形面积并,扫描线
    [hdu4628 Pieces]二进制子状态,DP
    [hdu4627 The Unsolvable Problem]数论
    [hdu4622 Reincarnation]后缀数组
    [hdu5379 Mahjong tree]dfs计数
    [hdu5375 Gray code]DP
    决策树
    算法题解之位运算
    算法题解之滑动窗口
  • 原文地址:https://www.cnblogs.com/cellular-automaton/p/7669954.html
Copyright © 2011-2022 走看看