JZOJ4970. B

题目大意

给定 NMK ,数组 a[N][M],b[N] ,定义

c[i]=j=0n1a[j][b[ij mod N]]

求第 K 大的c[i]

Data Constraint
这里写图片描述

题解

原式中有乘积的形式,非常的不优美,考虑去掉乘法。
注意到所有的 N 都是质数,所以我们可以用原根来转化,除了等于0的情况,都可以设i=gx, j=gy 于是原式可以表示成:

c[x]=y=1n1a[y][b[(x+y) mod (N1)]]

考虑枚举某一列来计算贡献,设当前计算第 i 列,那么
c[x]=y=1n1a[y][i]×[b[x+y]==i]

然后翻转一下:
c[x]=y=1n1a[y][i]×[b[x+y]==i]

这就是一个标准的卷积形式了。FFT解决。

时间复杂度: O(nlogn)

SRC

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

#define N 250000 + 10
#define M 600000 + 10
typedef long long ll ;
const double pi = acos(-1) , eps = 0.5 ;
struct Z {
    double x , y ;
    Z ( double X = 0 , double Y = 0 ) { x = X , y = Y ; }
} A[M] , B[M] , tmp[M] , tW[M] ;

int Ta1[11] = { 5 , 127 , 509 , 2039 , 8191 , 32749 , 65521 , 131071 , 249989 , 249973 , 249971 } ;
int Ta2[11] = { 2 , 3 , 2 , 7 , 17 , 2 , 17 , 3 , 2 , 5 , 6 } ;

ll a[N][6] , b[N] , c[N] ;
int rev[M] , P[N] ;
int n , m , k , G , len ;

Z operator + ( Z a , Z b ) { return Z( a.x + b.x , a.y + b.y ) ; }
Z operator - ( Z a , Z b ) { return Z( a.x - b.x , a.y - b.y ) ; }
Z operator * ( Z a , Z b ) { return Z( a.x * b.x - a.y * b.y , a.x * b.y + a.y * b.x ) ; }

bool cmp( ll a , ll b ) { return a > b ; }

int Read() {
    int ret = 0 ;
    char ch = getchar() ;
    while ( ch < '0' || ch > '9' ) ch = getchar() ;
    while ( ch >= '0' && ch <= '9' ) {
        ret = ret * 10 + ch - '0' ;
        ch = getchar() ;
    }
    return ret ;
}

void Pre() {
    for (int i = 0 ; i < 11 ; i ++ )
        if ( n == Ta1[i] ) { G = Ta2[i] ; break ; }
    P[0] = 1 ;
    for (int i = 1 ; i <= n ; i ++ ) P[i] = P[i-1] * G % n ;
    for ( len = 1 ; len < n + n ; len *= 2 ) ;
    for (int i = 0 ; i < len ; i ++ ) {
        tW[i] = Z( cos( i * 2 * pi / len ) , sin( i * 2 * pi / len ) ) ;
        int tp = 0 ;
        for (int x = i , k = 1 ; k < len ; k <<= 1 , x >>= 1 ) tp = (tp << 1) + (x & 1) ;
        rev[i] = tp ;
    }
}

void DFT( Z *a , int sig ) {
    for (int i = 0 ; i < len ; i ++ ) tmp[rev[i]] = a[i] ;
    for (int m = 2 ; m <= len ; m <<= 1 ) {
        int half = m / 2 ;
        for (int i = 0 ; i < half ; i ++ ) {
            Z W = tW[(sig*i*(len/m)+len)%len] ;
            for (int j = i ; j < len ; j += m ) {
                Z u = tmp[j] ;
                Z v = tmp[j+half] * W ;
                tmp[j] = u + v ;
                tmp[j+half] = u - v ;
            }
        }
    }
    for (int i = 0 ; i < len ; i ++ ) a[i] = tmp[i] ;
}

void FFT( Z *A , Z *B ) {
    DFT( A , 1 ) , DFT( B , 1 ) ;
    for (int i = 0 ; i < len ; i ++ ) A[i] = A[i] * B[i] ;
    DFT( A , -1 ) ;
    for (int i = 0 ; i < len ; i ++ ) A[i].x = A[i].x / len ;
}

int main() {
    freopen( "b.in" , "r" , stdin ) ;
    freopen( "b.out" , "w" , stdout ) ;
    n = Read() , m = Read() , k = Read() ;
    for (int j = 0 ; j < m ; j ++ ) {
        for (int i = 0 ; i < n ; i ++ ) {
            a[i][j] = Read() ;
        }
    }
    for (int i = 0 ; i < n ; i ++ ) b[i] = Read() ;
    Pre() ;
    for (int i = 0 ; i < m ; i ++ ) {
        for (int j = 0 ; j < len ; j ++ ) A[j] = B[j] = Z( 0 , 0 ) ;
        for (int j = 0 ; j < n - 1 ; j ++ ) {
            A[n-1-j] = Z( a[P[j]][i] , 0 ) ;
            B[j] = b[P[j]] == i ? Z( 1 , 0 ) : Z( 0 , 0 ) ;
        }
        FFT( A , B ) ;
        for (int j = 0 ; j < len ; j ++ ) c[P[j%(n-1)]] += (int)(A[j].x + eps) ;
    }
    for (int i = 1 ; i < n ; i ++ ) c[0] += a[i][b[0]] ;
    for (int i = 0 ; i < n ; i ++ ) c[i] += a[0][b[0]] ;
    sort( c , c + n , cmp ) ;
    printf( "%lld\n" , c[k-1] ) ;
    return 0 ;
}

以上.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值