2022 Nioke winter vacation algorithm basic training camp 4, subsequence weight product (enumeration, Euler power reduction)

Portal
insert image description here
idea:

We can observe that after sorting , if the maximum and minimum values ​​are determined, the number in the middle has no effect on the result. Therefore, in this way, the maximum and minimum values ​​can be enumerated, and the middle part is reduced in At the same time, turn zero into an integer , then enumerate the length of the interval, combine the bases with the same power, and divide the two numbers by the inverse each time.

Euler's power reduction:

Request ab% pa ^ b \ \% \ pab %p  process, ifbbb is a relatively large number (may be in the form of a power), you can letbbb pair( p − 1 ) (p-1)(p1 ) Taking the modulo does not affect the correctness of the final answer (provided thataaa andppp is relatively prime), this modulo is called Euler's power reduction (according to Fermat's little theorem, whenaaa andppWhen p is relatively prime, there isap − 1 % p = 1 a^{p-1}\ \%\ p=1ap1 % p=1

Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[200010];
int mod=1e9+7;

ll power(ll a,ll b,ll mod){
    
    
    a%=mod;
    ll res=1;
    while(b){
    
    
        if(b&1)res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}

ll inv(ll x,ll mod){
    
    
    return power(x,mod-2,mod);
}

int main(){
    
    
    int n;
    cin>>n;
    ll res=1,cnt=1,tmp=1;
    for(int i=0;i<n;i++){
    
    
        cin>>a[i];
        tmp=tmp*a[i]%mod*a[i]%mod;
    }
    sort(a,a+n);
    ll l=0,r=n-1;
    //子序列长度为1的情况
    res=tmp;
    while(l<n-1){
    
    
        tmp=tmp*inv(a[l++],mod)%mod*inv(a[r--],mod)%mod;
        res=res*power(tmp%mod,power(2,l-1,mod-1),mod)%mod%mod;
    }
    cout<<res;
}

Guess you like

Origin blog.csdn.net/qq_45550375/article/details/122839209