牛客练习赛42C(枚举权值)
思路:既然无法枚举每个情况,那就枚举每个出现过的权值,加和。那么每个权值出现了多少次呢?用总数减去一次都选不中这个数的次数即可,类似概率的方法。
#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(枚举权值)的更多相关文章
- 牛客练习赛16 F 选值【二分/计数】
链接:https://www.nowcoder.com/acm/contest/84/F 来源:牛客网 题目描述 给定n个数,从中选出三个数,使得最大的那个减最小的那个的值小于等于d,问有多少种选法. ...
- 牛客练习赛16 E - 求值
题目大意: 链接:https://www.nowcoder.com/acm/contest/84/E 给定n个数字a1, a2, ..., an. 定义f(l, r) = al | al+1| ... ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 最小生成树--牛客练习赛43-C
牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...
- 牛客练习赛22C Bitset
牛客练习赛22C 一共有 n个数,第 i 个数是 xi xi 可以取 [li , ri] 中任意的一个值. 设 ,求 S 种类数. 感觉二进制真是一个神奇的东西. #include <iost ...
- 牛客练习赛51 **E-数列** (二分,贪心,构造)
牛客练习赛51 E-数列 链接:https://ac.nowcoder.com/acm/contest/1083/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...
- 牛客练习赛52 | C | [烹饪] (DP,裴蜀定理,gcd)
牛客练习赛52 C 烹饪 链接:https://ac.nowcoder.com/acm/contest/1084/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...
- 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B
牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...
- 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A
牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...
随机推荐
- 快速上手Ubuntu之安装篇——安装win7,Ubuntu16.04双系统【转】
本文转载自:http://blog.csdn.net/qq_28205153/article/details/52203512 Linux可以说是开发者的系统,对于开发者来说,Linux发行版不仅为我 ...
- 查看mysql连接情况,以及连接超时时间设置
查看连接: show processlist; 只展示100个连接 show full processlist; 展示所有连接 查看超时时间: show variables like '% ...
- Tensorflow基础知识
基本知识 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...
- c++11 右值引用和移动语义
什么是左值.右值 最常见的误解: 等号左边的就是左值,等号右边的就是右值 左值和右值都是针对表达式而言的, 左值是指表达式结束后依然存在的持久对象 右值是指表达式结束时就不再存在的临时对象区分: 能对 ...
- C++之remove和remove_if
一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: remove只是通过迭代器的指针向前 ...
- redis 操作使用
/*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...
- 关于数组array的一些误区
$arr1 = array(1,2,3,4); $arr2 = array(5,6,7,8,9,10); var_dump ( $arr1 + $arr2); //输出数组形式的(1,2,3,4 ...
- JQuery扩展插件Validate—5添加自定义验证方法
从前面的示例中不难看出validate中自带的验证方法足以满足一般的要求,对于特别的要求可以使用addMethod(name,method,message)添加自定义的验证规则,下面的示例中添加了一个 ...
- u盘启动安装系统
七彩虹主板如何设置U盘启动,本文就以七彩虹CG41主板为例详细的讲讲U盘启动设置方法. 几天前,想用U盘启动的时候,发现CG41主板启动顺序里找不到USB项,Boot Device Priority( ...
- Run-Time Check Failure #3 - The variable 'p' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'p' is being used without being initialized. 运行时检查失败 变量p没有初 ...