Inclusion and exclusion thematic summary

Inclusion and exclusion thematic summary

A:How many integers can you find

Too much water, but there are details

\[\ \]

\[\ \]

B: Eddy's hobby

Enum value of k, k-th root directly open, in order to prevent the explosion precision, hand me a bit

For receiving repellent factor is then, either directly coefficient sets Mobius


const int N=4e5+10;
const ll INF=(1ll<<62)-1;

ll n;

ll qsm(ll x,ll k) {
    ll res=1;
    while(k) {
        if(k&1) {
            double t=(double)res*x;
            if(t>INF) {
                res=INF;
                break;
            }
            else res=res*x;
        }
        double t=(double)x*x;
        if(t>INF) x=INF;
        else x=x*x;
        k>>=1;
    }
    return res;
}


ll yroot(ll x,ll y){
    ll l=1,r=2e9,res=1;
    while(l<=r) {
        ll mid=(l+r)>>1;
        if(qsm(mid,y)<=x) res=mid,l=mid+1;
        else r=mid-1;
    }
    return res;
}

ll dp[100];



int main() {
    while(~scanf("%lld",&n)) {
        ll res=0;
        rep(i,2,60) dp[i]=yroot(n,i)-1;
        drep(i,60,2) for(int j=i+i;j<=60;j+=i) dp[i]-=dp[j];//k=i的情况会在j中被多次计算
        rep(i,2,60) res+=dp[i];
        printf("%lld\n",res+1);
    }
}

\[ \ \]

\[ \ \]

C: Coprime

Raised n, m prime factors, then half the answer, inclusion and exclusion straight binary enum


const int N=4e5+10;
const ll INF=(1ll<<62)-1;

int k;

int fac[N],n;

void Div(int x) {
    for(int i=2;i*i<=x;++i) if(x%i==0) {
        fac[n++]=i;
        while(x%i==0) x/=i;
    }
    if(x>1) fac[n++]=x;
}


ll Check(ll mid){ 
    ll res=0;
    rep(S,0,(1<<n)-1) {
        ll t=1,cnt=0;
        rep(i,0,n-1) if(S&(1<<i)) cnt^=1,t=t*fac[i];
        if(cnt) res-=mid/t;
        else res+=mid/t;
    }
    return res;
}

int main(){
    rep(kase,1,rd()) {
        n=0;Div(rd()),Div(rd());
        k=rd();
        sort(fac,fac+n);
        n=unique(fac,fac+n)-fac;
        ll l=1,r=1e15,res=-1;
        while(l<=r) {
            ll mid=(1ll*l+1ll*r)>>1;
            if(Check(mid)>=k) res=mid,r=mid-1;
            else l=mid+1;
        }
        printf("Case %d: %lld\n",kase,res);
    }
}

\[ \ \]

\[ \ \]

D: GCD

First, the outer zone may be accommodated repellent

Then for each \ (1 \ leq i \ leq n \) binary enumeration inclusion and exclusion \ (1..m \) of the prime number

const int N=1e5+10;
const ll INF=(1ll<<62)-1;

int a,b,c,d,k;
vector <int> fac[N];


ll Solve(int n,int m,int k) {
    if(!k||!n||!m) return 0;
    n/=k,m/=k;
    if(n<m) swap(n,m);
    ll res=0;
    rep(i,1,n) {
        int t=min((int)i,m);
        int k=fac[i].size();
        rep(j,0,(1<<k)-1){
            ll x=1,cnt=0;
            rep(o,0,k-1) if(j&(1<<o)) x=x*fac[i][o],cnt^=1;
            if(cnt) res-=t/x;
            else res+=t/x;
        }
    }
    return res;
}



int main(){
    rep(i,2,N-1) if(!fac[i].size()) for(int j=i;j<N;j+=i) fac[j].push_back((int)i);
    rep(kase,1,rd()) {
        a=rd(),b=rd(),c=rd(),d=rd(),k=rd();
        ll res=Solve(b,d,k)-Solve(a-1,d,k)-Solve(b,c-1,k)+Solve(a-1,c-1,k);
        printf("Case %d: %lld\n",kase,res);
    }
}

\[\ \]

\[ \ \]

E: Co-prime

Have appeared before the basic idea, skip it

\[ \ \]

\[ \ \]

F: Frogs

The problem is still very little things right

First you have to know that such a thing
for any of \ (gcd (the n-, m) = 1 \) , $ \ lbrace the n-MOD m * i | i \ in the Z-\ rbrace \ (= \) {0, 1 ,. ., m-1} $

In fact, then for any \ (GCD (n-, m) = G \) , $ \ lbrace n- I MOD m | I \ in the Z \ rbrace \ (= \) {0, G, G 2, ..,. 1-m } $

So each can come to a place on the frog is determined, provided these \ (GCD \) values \ (a [1..n] \)

This time we simulate what binary enumeration process that took some number of them obtained their \ (LCM (Lowest \ the Common \ Multiple) \) , plus when the number is an odd number, subtract an even number

That is, when you choose a multiple number each time, taking the opposite sign of the coefficient number, and \ (LCM \) is not usually the case the number, i.e. \ (m \) number of factors

So dp direct transfer coefficient can seek

const int N=1e4+10;

ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b); }

int n,m;
int a[N];
int fac[N],c;
ll dp[N];
int lcm[2000][2000];

int gcd(int a,int b){ return b==0?a:gcd(b,a%b); }

int main(){
    rep(kase,1,rd()) {
        n=rd(),m=rd();
        rep(i,1,n) a[i]=gcd(rd(),m);
        sort(a+1,a+n+1);
        n=unique(a+1,a+n+1)-a-1;
        c=0;
        rep(i,1,sqrt(m+0.5)) if(m%i==0) {
            fac[++c]=i;
            if(i*i!=m) fac[++c]=m/i;
        }
        sort(fac+1,fac+c+1);
        rep(i,1,c) dp[i]=0;
        rep(i,1,n) a[i]=lower_bound(fac+1,fac+c+1,a[i])-fac;
        rep(i,1,c) rep(j,1,c) lcm[i][j]=lower_bound(fac+1,fac+c+1,fac[i]/gcd(fac[i],fac[j])*fac[j])-fac;
        rep(i,1,n) {
            int x=a[i];
            drep(j,c,1) {
                dp[lcm[x][j]]-=dp[j];
            }
            dp[a[i]]++;
        }
        ll ans=0;
        rep(i,1,c) ans+=1ll*dp[i]*(m/fac[i])*(m-fac[i])/2;
        printf("Case #%d: %lld\n",kase,ans);
    }
}

\[ \ \]

\[ \ \]

\[ \ \]

\[ \ \]

G: The Monkey King

At first I thought I could not do when \ (n \ ln \ n \ ) write, head wants to break up. . .

Enumeration big monkey took \ (x \) months, then the inclusion-exclusion than other people take his situation of

Statistics, when there is enumerated at least \ (I \) individuals than he, the answer is \ (C (n + mx * (i + 1) -1, m-2) \)

In fact seeking a combination of flapper law, then you can smooth the inclusion-exclusion

int n,m;
ll po[N]={1},Inv[N]={1,1};

ll C(int n,int m){ 
    if(n<0||m<0||n<m) return 0;
    return po[n]*Inv[m]%P*Inv[n-m]%P;
}

ll Solve(int x) {
    ll ans=C(n+m-x-2,m-2);
    rep(i,1,min(m-1,n/x-1)) {
        if(i&1) (ans=ans-C(m-1,i)*C(n+m-x*(i+1)-2,m-2))%=P;
        else ans=(ans+C(m-1,i)*C(n+m-x*(i+1)-2,m-2))%P;
    }
    return ans=(ans%P+P)%P;
}


int main(){
    rep(i,1,N-1) po[i]=po[i-1]*i%P;
    rep(i,2,N-1) Inv[i]=(P-P/i)*Inv[P%i]%P;
    rep(i,1,N-1) Inv[i]=Inv[i]*Inv[i-1]%P;
    rep(kase,1,rd()) {
        n=rd(),m=rd();
        if(m==1) {
            puts("1");
            continue;
        }
        ll ans=0;
        rep(i,(n-1)/m+1,n) ans+=Solve(i);
        ans%=P;
        ans=(ans%P+P)%P;
        printf("%lld\n",ans);
    }
}

\[ \ \]

\[ \ \]

H: Y sequence

The problem is not really the quality of

First, it would more a problem than a dichotomy and B, but if you really write half of the TLE! Iterations must write

Followed by handwriting can not be open-th root, and can only be used pow function, but also the accuracy of explosion


const ll INF=(1ll<<62)-1;

ll k;
int r;

int w[100],notpri[100],pri[100],cp;
int num[100],maxpri[100],nc;


inline ll Solve(ll n) {
    ll res=0;
    rep(i,1,nc) if(maxpri[i]<=r) res+=1ll*w[i]*((ll)pow(n+0.1,1.0/num[i])-1);
    return n-res-1;
}




int main() {
    rep(i,2,65) {
        if(!notpri[i]) {
            pri[++cp]=i;
            for(int j=i+i;j<=65;j+=i) notpri[j]=1;
        }
    }
    rep(i,2,65) {
        int x=i,t=-1,ma=0;
        rep(j,1,cp) {
            int cnt=0;
            while(x%pri[j]==0) cnt++,x/=pri[j];
            if(cnt>1) {
                t=0;
                break;
            }
            if(cnt) t=-t,ma=max(ma,pri[j]);
        }
        if(t) w[++nc]=t,num[nc]=i,maxpri[nc]=ma;
    }
    rep(kase,1,rd()){
        scanf("%lld",&k);r=rd();
        ll ans=k;
        while(1) {
            ll tmp=Solve(ans);
            if(k==tmp) break;
            ans+=k-tmp;
        }
        printf("%lld\n",ans);
    }
}


\[ \ \]

\[ \ \]

I: MG loves string

This practice is also a visual problem very Cheetos, I used the matrix multiplication

First, a plurality of rings 26 letters, only a maximum loop length of six kinds of species

So I direct these six kinds of long-shaped pressure ring, matrix power to fast



const int N=1e5+10,P=1e9+7;

int n;
int nxt[26],c[26];
char str[30];
int vis[27];
int A,k[N],id[N];
int cnt;

ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }

struct Mat{
    int a[1<<6][1<<6];
    void init(){ rep(i,0,A) rep(j,0,A) a[i][j]=0; }
    void Get1(){ rep(i,0,A) a[i][i]=1; }
    Mat operator * (const Mat x) const{
        Mat res; res.init();
        rep(i,0,A) rep(j,0,A) rep(o,0,A) (res.a[i][o]+=1ll*a[i][j]*x.a[j][o]%P)%=P;
        return res;
    }
}res,x;
ll f[1][1<<6],ans[1][1<<6];




int main(){
    rep(kase,1,rd()) {
        n=rd();
        scanf("%s",str);
        rep(i,0,25) nxt[i]=str[i]-'a';
        cnt=0;
        memset(vis,0,sizeof vis);
        rep(i,0,25) {
            int p=i;c[i]=0;
            do {
                p=nxt[p];
                c[i]++;
            } while(p!=i);
            if(!vis[c[i]]) {
                vis[c[i]]=1;
                id[c[i]]=cnt;
                k[cnt++]=c[i];
            }
        }
        A=(1<<cnt)-1;
        x.init(),res.init();res.Get1();
        rep(i,0,A) {
            rep(j,0,25) {
                x.a[i][i|(1<<id[c[j]])]++;
            }
        }
        while(n) {
            if(n&1) res=res*x;
            x=x*x;
            n>>=1;
        }
        memset(f,0,sizeof f);memset(ans,0,sizeof ans);
        f[0][0]=1;
        rep(i,0,0) rep(j,0,A) rep(o,0,A) (ans[i][o]+=1ll*f[i][j]*res.a[j][o]%P)%=P;
        ll Ans=0;
        rep(S,0,A) {
            ll t=1;
            rep(i,0,cnt-1) if(S&(1<<i)) t=t*k[i]/gcd(t,k[i]);
            (Ans+=t*ans[0][S]%P)%=P;
        }
        printf("%lld\n",Ans);
    }
}

\[ \ \]

\[ \ \]

J: Harry And Magic Box

dp inclusion and exclusion like, \ (dp [I] [J] \) represents \ (I \) row \ (J \) columns are put in the embodiment, enumerated less than \ (I \) or less than \ (j \) program to lose enough

int n,m,q;
ll Inv[N*N]={1,1},po[N*N]={1};
ll B[N][N],p2[N*N]={1};

inline ll C(int n,int m){
    if(n<0||m<0||n<m) return 0;
    return po[n]*Inv[m]%P*Inv[n-m]%P;
}



int main(){
    rep(i,1,N*N-1) po[i]=po[i-1]*i%P,p2[i]=p2[i-1]*2%P;
    rep(i,2,N*N-1) Inv[i]=(P-P/i)*Inv[P%i]%P;
    rep(i,1,N*N-1) Inv[i]=Inv[i]*Inv[i-1]%P;
    rep(i,0,50) {
        rep(j,0,50) {
            B[i][j]=p2[i*j];
            rep(a,0,i) {
                rep(b,0,j) if(a!=i||b!=j) {
                    (B[i][j]-=C(i,a)*C(j,b)%P*B[a][b]%P)%=P;
                }
            }
            B[i][j]=(B[i][j]%P+P)%P;
        }
    }
    while(~scanf("%d%d",&n,&m)) printf("%lld\n",B[n][m]);
}

\[ \ \]

\[ \ \]

\[ \ \]

K: Count the Grid

It looks very scary, but in fact better

The general idea is to give each point a given \ (\ lim) value, find the number of programs under the constraints matrix of inclusion and exclusion

Binary enumeration is then, for each enumerated to \ (Lim \) values to be minus one

In fact, the total number of elected in \ (\ lim) value minus the program thus obtained is the maximum value of \ (lim \) of the program

How statistical matrix \ (\ lim) value, then cycle directly discrete assignment just fine, but because often stuck so I had to add some optimization

Code written more bizarre is not recommended to look, you can take it on the beat

#include<bits/stdc++.h>
using namespace std;

#define reg register
typedef long long ll;
#define rep(i,a,b) for(reg int i=a;i<=b;++i)
#define drep(i,a,b) for(reg int i=a;i>=b;--i)



char IO;
int rd(){
    int s=0,f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar()));
    return f?-s:s;
}

const int N=110,P=1e9+7;


int n,m,k,q;
int hx[N],cx,hy[N],cy;

struct REC{
    int x1,y1,x2,y2,lim;
    void Get() {
        x1=rd(),y1=rd();
        x2=rd(),y2=rd();
        hx[++cx]=x1,hx[++cx]=x2;
        if(x1>1) hx[++cx]=x1-1;
        if(x1<n) hx[++cx]=x1+1;
        if(x2>1) hx[++cx]=x2-1;
        if(x2<n) hx[++cx]=x2+1;
        hy[++cy]=y1,hy[++cy]=y2;
        if(y1>1) hy[++cy]=y1-1;
        if(y1<m) hy[++cy]=y1+1;
        if(y2>1) hy[++cy]=y2-1;
        if(y2<m) hy[++cy]=y2+1;
        lim=rd();
    }
    void Hash() {
        x1=lower_bound(hx+1,hx+cx+1,x1)-hx;
        x2=lower_bound(hx+1,hx+cx+1,x2)-hx;
        y1=lower_bound(hy+1,hy+cy+1,y1)-hy;
        y2=lower_bound(hy+1,hy+cy+1,y2)-hy;
    }
}R[N];

inline ll qsm(reg ll x,reg int k){
    reg ll res=1;
    for(;k;k>>=1,x=x*x%P) if(k&1) res=res*x%P;
    return res;
}



int a[N][N],b[N][N],vis[N][N];
ll c[2][N][N];
ll Solve() {
    ll ans=1;
    rep(i,1,cx) rep(j,1,cy) a[i][j]=k;
    for(reg int i=0; i<q; ++i) rep(x,R[i].x1,R[i].x2) rep(y,R[i].y1,R[i].y2) a[x][y]=min(a[x][y],R[i].lim);
    rep(i,1,cx) rep(j,1,cy) ans=ans*qsm(a[i][j],(hx[i]-hx[i-1])*(hy[j]-hy[j-1]))%P;
    return ans;
}


int main(){
    int T=rd();
    rep(kase,1,T){
        n=rd(),m=rd(),k=rd(),q=rd();
        hx[cx=1]=n,hy[cy=1]=m;
        hx[++cx]=1,hy[++cy]=1;
        rep(i,0,q-1) R[i].Get();
        sort(hx+1,hx+cx+1),sort(hy+1,hy+cy+1);
        cx=unique(hx+1,hx+cx+1)-hx-1,cy=unique(hy+1,hy+cy+1)-hy-1;
        rep(i,0,q-1) R[i].Hash();
        rep(i,1,cx) rep(j,1,cy) b[i][j]=k;
        for(reg int i=0; i<q; ++i) rep(x,R[i].x1,R[i].x2) rep(y,R[i].y1,R[i].y2) b[x][y]=min(b[x][y],R[i].lim);
        rep(i,1,cx) rep(j,1,cy){
             c[0][i][j]=qsm(b[i][j],(hx[i]-hx[i-1])*(hy[j]-hy[j-1]))%P;
             c[1][i][j]=qsm(b[i][j]-1,(hx[i]-hx[i-1])*(hy[j]-hy[j-1]))%P;
             a[i][j]=b[i][j];
        }
        int A=(1<<q)-1;
        ll ans=0;
        rep(S,0,A) {
            int cnt=0;
            rep(i,0,q-1) if(S&(1<<i)) {
                cnt^=1;
                rep(x,R[i].x1,R[i].x2) rep(y,R[i].y1,R[i].y2) if(a[x][y]==R[i].lim) a[x][y]=R[i].lim-1,vis[x][y]=R[i].lim;
            }
            ll res=1;
            rep(i,1,cx) rep(j,1,cy) res=res*c[b[i][j]-a[i][j]][i][j]%P;
            if(cnt) ans-=res;
            else ans+=res;
            //cout<<S<<"  "<<Solve()<<endl;
            rep(i,0,q-1) if(S&(1<<i)) rep(x,R[i].x1,R[i].x2) rep(y,R[i].y1,R[i].y2) if(vis[x][y]) a[x][y]=vis[x][y],vis[x][y]=0;
        }
        ans=(ans%P+P)%P;
        printf("Case #%d: %lld\n",kase,ans);
    }
}

\[ \ \]

\[\ \]

L: Visible Trees

And in front of several basic questions empathy

#include<bits/stdc++.h>
using namespace std;

#define reg register
typedef long long ll;
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)



char IO;
int rd(){
    int s=0,f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar()));
    return f?-s:s;
}

const int N=1e5+10,P=1e9+7;


int n,m,k,q;
vector <int> fac[N];

ll Solve(int n,int m) {
    ll res=0;
    rep(i,1,n) {
        int k=fac[i].size();
        int x=min(i,m);
        rep(S,0,(1<<k)-1) {
            int cnt=0,t=1;
            rep(j,0,k-1) if(S&(1<<j)) t*=fac[i][j],cnt^=1;
            if(cnt) res-=x/t;
            else res+=x/t;
        }
    }
    //cout<<res<<endl;
    return res;
}






int main(){
    rep(i,2,N-1) if(!fac[i].size()) for(int j=i;j<N;j+=i) fac[j].push_back((int)i);
    rep(kase,1,rd()){
        n=rd(),m=rd();
        ll ans=Solve(n,m)+Solve(m,n)-1;
        printf("%lld\n",ans);
    }
}


\[ \ \]

\[ \ \]

M: A Simple Chess

Vault?

First, we discuss in empty \ (n, m \) hops on the board plan

Because must be the first to jump, so every time the coordinates will increase, set sideways jump \ (a \) times the vertical jump \ (b \) times

There

\[2*a+b=n\]

\[2*b+a=m\]

Solutions have two equations to obtain values ​​of a, b, and if it is an integer no solution

In fact, the program number is then \ (C (a + b, a) \)

Inclusion-exclusion process is still dp transfer will go through a multi-point barrier during each transfer, the inclusion-exclusion factor negated

However, due to the large scope of this problem, the number of combinations poor demand, in view of the module only 110119, so you can use \ (Lucas \) Theorem

\(C(n,m) mod \ p =C(n \ mod \ p,m \ mod \ p)*(n/p,m/p) mod \ p\)

After pretreatment directly to do

#include<bits/stdc++.h>
using namespace std;

#define reg register
typedef long long ll;
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)



char IO;
ll rd(){
    ll s=0,f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar()));
    return f?-s:s;
}

const int N=110,P=110119;


ll n,m;
int r;

struct NODE {
    ll x,y;
    bool operator < (const NODE __ )const{
        return x<__.x;
    }
    bool operator == (const NODE __) const{
        return x==__.x&&y==__.y;
    }
}A[N];
int k;

ll po[P]={1},Inv[P]={1,1};

ll C(ll n,ll m){
    if(n<0||m<0||n<m) return 0;
    if(n<P&&m<P) return po[n]*Inv[m]%P*Inv[n-m]%P;
    return C(n%P,m%P)*C(n/P,m/P)%P;
}




ll Calc(ll n,ll m) {
    if((n+m-2)%3!=0) return 0;
    if((2*n-m-1)%3!=0) return 0;
    ll a=(n+m-2)/3,b=(2*n-m-1)/3;
    return C(a,b);
}


//(i+j-2)/3
//(2*i-j-1)/3

ll dp[N];
int kase;
int main(){
    rep(i,1,P-1) po[i]=po[i-1]*i%P;
    rep(i,2,P-1) Inv[i]=(P-P/i)*Inv[P%i]%P;
    rep(i,1,P-1) Inv[i]=Inv[i-1]*Inv[i]%P;
    while(~scanf("%lld%lld%d",&n,&m,&r)) {
        k=0;
        A[++k]=(NODE){1ll,1ll};
        int f=0;
        rep(i,1,r) {
            ll x=rd(),y=rd();
            A[++k]=(NODE){x,y};
            if((x==n&&y==m)||(x==1&&y==1)) f=1;
        }
        if(f) {
            printf("Case #%d: 0\n",++kase);
            continue;
        }
        if(n==1&&m==1) {
            printf("Case #%d: 1\n",++kase);
            continue;
        }
        A[++k]=(NODE){n,m};
        sort(A+1,A+k+1);
        k=unique(A+1,A+k+1)-A-1;
        memset(dp,0,sizeof dp);
        dp[1]=1;
        rep(i,1,k) rep(j,i+1,k) if(A[j].x>A[i].x&&A[j].y>A[i].y) (dp[j]-=dp[i]*Calc(A[j].x-A[i].x+1,A[j].y-A[i].y+1)%P)%=P;
        ll ans=dp[k];
        ans=(P-ans)%P;
        ans=(ans%P+P)%P;
        printf("Case #%d: %lld\n",++kase,ans);
    }
}


\[ \ \]

\[ \ \]

N: TrickGCD

See this question thought for the entire sequence \ (gcd \) values of inclusion and exclusion

How to quickly find the number of sequence programs under a certain value limit it?

And direct prefix, \ (the n-\ the n-LN \) enumeration, quickly power can finally take a Mobius factor

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<iostream>
using namespace std;

#define reg register
typedef long long ll;
#define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i)

char IO;
int rd(){
    int s=0,f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar())) ;
    return f?-s:s;
}

const int N=2e5+10,P=1e9+7;


int n;
int a[N];

inline ll qsm(reg ll x,reg int k) {
    if(x==1||k<=0) return 1;
    reg ll res=1;
    for(;k;k>>=1,x=x*x%P) ((k&1)&&(res=res*x%P));
    return res;
}

int pri[N],cp;
int notpri[N];
int mo[N];

int main(){
    rep(i,2,N-1)  {
        if(!notpri[i]) pri[++cp]=i,mo[i]=1;
        rep(j,1,cp){
            int t=i*pri[j];
            if(t>=N) break;
            notpri[t]=1;
            if(i%pri[j]==0) {
                mo[t]=0;
                break;
            }
            mo[t]=-mo[i];
        }
    }
    int T=rd();
    rep(kase,1,T) {
        int Up=1e9,ma=0;
        scanf("%d",&n);
        rep(i,1,n) {
            int x; scanf("%d",&x);
            a[x]++;
            Up=min(Up,x); 
            ma=max(ma,x);
        }
        rep(i,Up,N-1) a[i]+=a[i-1];
        ll ans=0;
        for(reg int i=2;i<=Up;++i) if(mo[i]) {
            ll res=1;
            rep(j,1,ma/i) {
                res=res*qsm(j,a[(j+1)*i-1]-a[j*i-1])%P;
            }
            ans+=mo[i]*res;
        }
        rep(i,Up,N-1) a[i]=0;
        ans=(ans%P+P)%P;
        printf("Case #%d: %lld\n",kase,ans);
    }
}

\[ \ \]

\[ \ \]

A: Puzzled Elena

Flood problem

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

#define reg register
typedef long long ll;
#define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i)

char IO;
int rd(){
    int s=0,f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar())) ;
    return f?-s:s;
}

const int N=1e5+10,P=1e9+7;


int n;
struct Edge{
    int to,nxt;
}e[N<<1];
int head[N],ecnt;
void AddEdge(int u,int v){
    e[++ecnt]=(Edge){v,head[u]};
    head[u]=ecnt;
}

vector <int> fac[N];

int a[N],ans[N];
int c[N];
void dfs(int u,int f) {
    int n=fac[a[u]].size();
    ans[u]=0;
    rep(S,0,(1<<n)-1) {
        int t=1,cnt=0;
        rep(i,0,n-1) if(S&(1<<i)) cnt^=1,t*=fac[a[u]][i];
        if(cnt) ans[u]+=c[t];
        else ans[u]-=c[t];
    }
    rep(S,0,(1<<n)-1) {
        int t=1;
        rep(i,0,n-1) if(S&(1<<i)) t*=fac[a[u]][i];
        c[t]++;
    }
    for(int i=head[u];i;i=e[i].nxt) {
        int v=e[i].to;
        if(v==f) continue;
        dfs(v,u);
    }
    rep(S,0,(1<<n)-1) {
        int t=1,cnt=0;
        rep(i,0,n-1) if(S&(1<<i)) cnt^=1,t*=fac[a[u]][i];
        if(cnt) ans[u]-=c[t];
        else ans[u]+=c[t];
    }
}




int kase;
int main(){ 
    rep(i,2,N-1) if(!fac[i].size()) for(int j=i;j<N;j+=i) fac[j].push_back((int)i);
    while(~scanf("%d",&n)) {
        memset(c,0,sizeof c);memset(head,0,sizeof head);ecnt=0;
        rep(i,2,n) {
            int u=rd(),v=rd();
            AddEdge(u,v);
            AddEdge(v,u);
        }
        rep(i,1,n) a[i]=rd();
        dfs(1,0);
        printf("Case #%d:",++kase);
        rep(i,1,n) printf(" %d",ans[i]);
        puts("");
    }
}

\[ \ \]

\[ \ \]

P: Just Random

\ (O (1) \) Why the title in the last question

For two numbers \ (n-, m (n-\ Leq m) \) , and they are accumulated as \ (X \) is the number of programs actually there is a certain regular

When \ (x \ leq n \) , the program number \ (x + 1 \)

When (n \ leq x \ leq m \) \ , the program number \ (n + 1 \)

When \ (m \ leq x \) , the program number \ (max \ {0, n + m-x + 1 \} \)

Then the ojbk

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;

#define reg register
typedef long long ll;
#define rep(i,a,b) for(reg int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(reg int i=a,i##end=b;i>=i##end;--i)

char IO;
int rd(){
    int s=0,f=0;
    while(!isdigit(IO=getchar())) if(IO=='-') f=1;
    do s=(s<<1)+(s<<3)+(IO^'0');
    while(isdigit(IO=getchar())) ;
    return f?-s:s;
}

const int N=1e5+10;


ll a,b,c,d;
ll p,m;


ll Solve(ll a,ll b) {
    if(a<0||b<0) return 0;
    if(a>b) swap(a,b);
    ll ans=0,t;
    if(a>=m) {
        t=1ll*(a-m)/p*p+m;
        ans+=1ll*(m+t+2)*((t-m)/p+1)/2;
    }
    t=0;
    if(b>=m) t=(b-m)/p+1;
    if(a>=m) t-=(a-m)/p+1;
    ans+=t*(a+1);
    if(b>=m) {
        t=1ll*(b-m)/p*p+m;
        ans-=1ll*(2*a+2*b-m-t+2)*((t-m)/p+1)/2;
    }
    if(a+b>=m) {
        t=1ll*(a+b-m)/p*p+m;
        ans+=1ll*(2*a+2*b-m-t+2)*((t-m)/p+1)/2;
    }
    return ans;
}

ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }



int main(){ 
    int cnt=0;
    rep(i,0,0) rep(j,0,5) if((i+j)%3==2) cnt++;
    rep(kase,1,rd()) {
        a=rd(),b=rd(),c=rd(),d=rd(),p=rd(),m=rd();
        ll ans=Solve(b,d)-Solve(a-1,d)-Solve(b,c-1)+Solve(a-1,c-1);
        printf("Case #%d: ",kase);
        if(!ans) {
            puts("0/1");
            continue;
        }
        ll t=1ll*(b-a+1)*(d-c+1);
        ll g=gcd(t,ans);
        ans/=g,t/=g;
        printf("%lld/%lld\n",ans,t);
    }
}

Guess you like

Origin www.cnblogs.com/chasedeath/p/11403449.html