BZOJ2751HAOI2012容易題(easy) 快速冪快速乘

來源:互聯網
上載者:User
連結:
#include <stdio.h>int main(){    puts("轉載請註明出處[vmurder]謝謝");    puts("網址:blog.csdn.net/vmurder/article/details/44684583");}
題解:

詢問只有10萬個,所以有相同性質的連一塊的點很多。
所以我們把 109 10^9點分成最多 2∗105 2*10^5塊。然後就隨便亂搞了。

分成 2∗105 2*10^5塊的過程是先把點排個序,然後就對每個點暴力往下刪了。
然後分完了以後就是把所有的點的選擇個數 x x乘起來就行了。長度為 y y那就乘 xy x^y

然後可能會爆,所以需要快速乘,請見mul部分。 代碼:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 201000#define mod 1000000007using namespace std;struct Eli{    long long x,y,z;    void read(){cin>>x>>y;}    Eli(long long _x=0,long long _y=0,long long _z=0):x(_x),y(_y),z(_z){}    bool operator <(const Eli &A)const    {return x==A.x?y<A.y:x<A.x;}}eli[N],src[N];long long n,m,p,cnt;long long mul(long long a,long long b){    long long ret=0;    while(b)    {        if(b&1)ret=(ret+a)%mod;        a<<=1,a%=mod,b>>=1;    }    return ret;}long long power(long long a,long long b){    long long ret=1;    while(b)    {        if(b&1)ret=mul(ret,a);        a=mul(a,a),b>>=1;    }    return ret;}long long ans=0;long long lsum[N],rsum[N];int main(){    int i,j,k;    cin>>n>>m>>p;    for(i=1;i<=p;i++)eli[i].read();    sort(eli+1,eli+p+1);    long long sum=n*(n+1)/2;    long long t=sum,last=1ll,r=n;    for(i=1;i<=p;i++)    {        if(eli[i].x!=last)        {            src[++cnt]=Eli(1,r%mod,t%mod);            k=eli[i].x-last;            if(k>1)src[++cnt]=Eli(k-1,n%mod,sum%mod);            last=eli[i].x,r=n-1,t=sum-eli[i].y;        }        else {            if(eli[i].y==eli[i-1].y)continue;            r--,t-=eli[i].y;        }    }    src[++cnt]=Eli(1,r%mod,t%mod);    k=m-last;    if(k)src[++cnt]=Eli(k,n%mod,sum%mod);/*  lsum[0]=rsum[cnt+1]=1ll;    for(i=1;i<=cnt;i++)    {        lsum[i]=lsum[i-1]*power(src[i].y,src[i].x)%mod;    }    for(i=cnt;i;i--)    {        rsum[i]=rsum[i+1]*power(src[i].y,src[i].x)%mod;    }    for(i=1;i<=cnt;i++)    {        ans=(ans+src[i].x*src[i].z%mod*lsum[i-1]%mod*rsum[i+1]%mod)%mod;    }*/        ans=1;    for(i=1;i<=cnt;i++)    {        ans=ans*power(src[i].z,src[i].x)%mod;    }    cout<<ans<<endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.