传送门

思路:既然无法枚举每个情况,那就枚举每个出现过的权值,加和。那么每个权值出现了多少次呢?用总数减去一次都选不中这个数的次数即可,类似概率的方法。

 #include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = ;
const int mod = 1e9 + ;
int n, m, tot, invn, U, ans;
int id[maxn][maxn], val[maxn * maxn], f[maxn * maxn];
struct node {
int val, x, y; bool operator < (const node &rhs) const {
return val < rhs.val;
}
}a[maxn * maxn]; inline int ksm(int a, int b) {
int ret = ;
for (; b; b >>= ) {
if (b & ) ret = (ll)ret * a % mod;
a = (ll)a * a % mod;
}
return ret;
} int main() {
ios_base::sync_with_stdio();
cin.tie(); cin >> n >> m;
invn = ksm(n, mod - ), U = ksm(n, m); for (int i = ; i <= m; i++) {
for (int j = ; j <= n; j++) {
int cid = (i - ) * n + j;
cin >> a[cid].val;
a[cid].x = i, a[cid].y = j;
}
}
sort(a + , a + + n * m);
for (int i = ; i <= n * m; i++) {
tot += (i == || a[i - ].val != a[i].val);
id[a[i].x][a[i].y] = tot;
val[tot] = a[i].val;
} for (int i = ; i <= tot; i++) f[i] = ;
for (int i = ; i <= m; i++) {
sort(id[i] + , id[i] + + n);
for (int j = , pos; j <= n; j = pos + ) {
pos = j;
while (pos < n && id[i][pos] == id[i][pos + ]) pos++;
f[id[i][j]] = (ll)f[id[i][j]] * (n - pos + j - ) % mod * invn % mod;
}
} for (int i = ; i <= tot; i++) {
ans = ((ll)ans + (ll)val[i] * (U - (ll)f[i] * U % mod + mod) % mod) % mod;
}
cout << ans << endl; return ;
}

牛客练习赛42C(枚举权值)的更多相关文章

  1. 牛客练习赛16 F 选值【二分/计数】

    链接:https://www.nowcoder.com/acm/contest/84/F 来源:牛客网 题目描述 给定n个数,从中选出三个数,使得最大的那个减最小的那个的值小于等于d,问有多少种选法. ...

  2. 牛客练习赛16 E - 求值

    题目大意: 链接:https://www.nowcoder.com/acm/contest/84/E 给定n个数字a1, a2, ..., an. 定义f(l, r) = al | al+1| ... ...

  3. 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D

    目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...

  4. 最小生成树--牛客练习赛43-C

    牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 ​ 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...

  5. 牛客练习赛22C Bitset

    牛客练习赛22C 一共有 n个数,第 i 个数是 xi  xi 可以取 [li , ri] 中任意的一个值. 设 ,求 S 种类数. 感觉二进制真是一个神奇的东西. #include <iost ...

  6. 牛客练习赛51 **E-数列** (二分,贪心,构造)

    牛客练习赛51 E-数列 链接:https://ac.nowcoder.com/acm/contest/1083/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

  7. 牛客练习赛52 | C | [烹饪] (DP,裴蜀定理,gcd)

    牛客练习赛52 C 烹饪 链接:https://ac.nowcoder.com/acm/contest/1084/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

  8. 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B

    牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...

  9. 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A

    牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...

随机推荐

  1. 拓展gcd求不定方程通解

    void gcd(LL a,LL b,LL &d,LL &x,LL &y){ ){d=a;x=;y=;return;} gcd(b,a%b,d,x,y); int t=x; x ...

  2. Codeforces Round #394 (Div. 2) B. Dasha and friends —— 暴力 or 最小表示法

    题目链接:http://codeforces.com/contest/761/problem/B B. Dasha and friends time limit per test 2 seconds ...

  3. Java 出现“Illegal key size”错误的解决方案

    用AES加密时出现"java.security.InvalidKeyException: Illegal key size"异常. 如果密钥大于128, 会抛出上述异常.因为密钥长 ...

  4. iOS7默认状态栏文字颜色为黑色,项目需要修改为白色。

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的 AppDelegate中在 didFinishLaun ...

  5. nginx ,node 配置项目

    nginx ,node 配置项目 1.安装好node,npm 2.安装cnpm,-g是全局的 sudo npm install -g cnpm --registry=https://registry. ...

  6. DGA域名——可以每天只生成一个域名,因此最多存在365个 DGA域名;

    Mirai变种中的DGA 分享到: 发布时间:2016-12-12 16:02:57 作者:360网络安全研究院 投稿方式:发送邮件至linwei#360.cn,或登陆网页版在线投稿 那个导致美国断网 ...

  7. java 基于百度地图API GPS经纬度解析地址

    首先这是百度地图api 的接口地址,基于接口的参数,不过多介绍,其中都提供相应的介绍: http://lbsyun.baidu.com/index.php?title=webapi/guide/web ...

  8. POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)

    In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edg ...

  9. kali本機安裝openvas的血淚史復盤

    安裝openvas的血淚史 因爲學習的需要,需要裝openvas,但是在虛擬機裏面,無論怎麼更新跟新源,總是會有問題,一氣之下,便不用虛擬機了,將自己的物理機刷成了kali機,從此便進了一個大坑. 安 ...

  10. RQNOJ魔法石之恋

    魔法石之恋 (stone.pas/c/cpp) [问题描述] 在<Harry Potter and the Sorcerer's Stone>中,想得到魔法石,必须要通过许许多多的测试和游 ...