P2220 [HAOI2012] easy problems [Primary Mathematics]

Title Description

To make everyone happy, a little Q deliberately simple questions (easy) consider themselves to meet everyone, this simple question is described as follows:

There are a number of columns A known for all A [i] is 1 to the natural number n, and know for some A [i] which value can not be taken, we define the number of the product for a series of columns of the product of all the elements, requires you Determine all possible values ​​of the product of the number of columns and mod 1000000007, it is not very simple? Ha ha!

Resolve

Label inside number theory is a lie (fog), is it because there is a mod it? ? ?

This question is very simple, the first observation data range, found that k is relatively small, then we make a fuss about it.

The impact of our wish to find no limit to the sum of the conditions, minus the constraints caused by this complexity on the relationship with k.

Suppose \ (P \) is in addition to the position (I \) \ product of the remaining positions, then

\ (Years = 1 + 2 * p * p + \ cdots + n * p = (\ frac {n (n + 1)} {2}) * p \)

For any position, it is clearly this thing.

Then \ (ANS = {(\ {n-FRAC (n-+. 1)} {2})} m ^ \) .

Or as appreciated by multiplying pile items, \ (ANS = (. 1 + 2 + \ + n-cdots) * (. 1 + 2 + \ + n-cdots) * \ cdots \)

A total of \ (m \) key, it becomes the top of that thing.

Then we consider restrictions.

Firstly sorted by location restrictions, to a position \ (I \) all the constraints, and if they are to \ (\ SUM) , it is easy to see that they reject the answer to \ (sum * {(\ frac {n (n-+. 1)} {2}) ^ {}} *. 1 mi The now-\) . Wherein \ (now \) is thinned constraints \ (1 \ sim i-1 \) PRODUCTS. \ (now \) initial value of 1, each time multiplied by the culling \ (\ {n-FRAC (+ n-1)} {2} -sum \) .

Moreover, since no Excluding the impact of each position on the final result, discrete.

Complexity \ (O (klogm klogk +) \) .

I write more code Kichiku, but please forgive me heavyweights QWQ.

Reference Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 100010
#define MOD 2520
#define E 1e-12
#define ll long long
#define mod 1000000007
#define div 500000004
using namespace std;
inline int read()
{
    int f=1,x=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
ll n,m,k,sum[N];
set<int> s[N];
struct node{
    int pos,x;
    inline bool operator<(const node &a)const{
        return a.pos>pos;
    }
}a[N];
inline ll qp(ll a,ll b){ll ans=1;for(;b;b>>=1){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;}return ans%mod;} 
int main()
{
    n=read(),m=read(),k=read();
    for(int i=1;i<=k;++i)
        a[i].pos=read(),a[i].x=read();
    sort(a+1,a+k+1);
    int cnt=1,pos=a[1].pos;
    for(int i=1;i<=k;++i){
        if(pos==a[i].pos){
            if(s[cnt].find(a[i].x)!=s[cnt].end()) continue;
            s[cnt].insert(a[i].x),sum[cnt]=(sum[cnt]%mod+a[i].x%mod)%mod;
        }
        else s[++cnt].insert(a[i].x),sum[cnt]=(sum[cnt]%mod+a[i].x%mod)%mod;
        pos=a[i].pos;
    }//脑抽写的set离散化
    ll tmp=(n%mod*(n+1)%mod*div%mod)%mod;
    ll ans=qp(tmp,m)%mod;
    ll now=1;
    for(int i=1;i<=cnt;++i){
        ans=(ans%mod-sum[i]%mod*qp(tmp,m-i)%mod*now%mod+2*mod)%mod;
        now=(now%mod*(tmp%mod-sum[i]%mod+2*mod)%mod)%mod;
    }
    printf("%lld\n",ans%mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11794173.html
Recommended