出题的诀窍

题目链接:https://ac.nowcoder.com/acm/contest/393/C

题解:

由于他是在每一行选取一个元素,然后纵向来比较,这里行的顺序是不会影响的,所以我们将每一个数存入哈希表中,然后对每一个数来进行考虑。

第一行的数,对答案的贡献为mn-1,而第二行对答案的贡献为mn-2*(m-1)...以此类推。

这里注意对同一行有多个相同元素的情况考虑一下。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = , M = , MOD = 1e9 + ;
ll a[N][N], pm[N];
int n, m;
struct Edge {
ll v, next, i;
} e[N * N];
ll head[M], tot, h[M];
ll f[N * N], d[N * N];
void adde(ll u, ll v, ll i) {
e[tot].i = i;
e[tot].v = v;
e[tot].next = head[u];
head[u] = tot++;
}
void hsh(ll x, ll y) {
ll now = x % M;
while(h[now] != - && h[now] != x) {
now += ;
if(now >= M)
now -= M;
}
h[now] = x;
adde(now, x, y);
}
int main() {
ios::sync_with_stdio(false);
cin.tie();
cin >> m >> n;
memset(head, -, sizeof(head));
memset(h, -, sizeof(h));
pm[] = ;
for(int i = ; i <= n; i++)
pm[i] = pm[i - ] * m % MOD;
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
cin >> a[i][j];
hsh(a[i][j], i);
}
}
ll ans = , cnt, num, pr;
for(int x = ; x < M; x++) {
if(h[x] != -) {
pr = ;
cnt = ;
num = ;
for(int i = head[x]; i != -; i = e[i].next) {
d[++cnt] = e[i].i;
}
for(int i = ; i <= cnt; i++) {
if(i == || d[i] != d[i - ])
f[++num] = ;
else
f[num]++;
}
for(int i = ; i <= num; i++) {
ans += pm[n - i] * pr % MOD * f[i] % MOD * h[x] % MOD;
pr = pr * (m - f[i]) % MOD;
ans %= MOD;
}
}
}
cout << ans;
return ;
}

牛客练习赛42 出题的诀窍(数学+hash)的更多相关文章

  1. 牛客练习赛42 C 出题的诀窍 (贡献,卡常)

    牛客练习赛42 C 出题的诀窍 链接:https://ac.nowcoder.com/acm/contest/393/C来源:牛客网 题目描述 给定m个长为n的序列a1,a2,-,ama_1 , a_ ...

  2. 牛客练习赛42(A,B)

    A:链接:https://ac.nowcoder.com/acm/contest/393/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 6 ...

  3. 牛客练习赛51 C 勾股定理 (数学,结论)

    链接:https://ac.nowcoder.com/acm/contest/1083/C来源:牛客网 题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角 ...

  4. 牛客练习赛42 C 出题的诀窍

    题目链接:https://ac.nowcoder.com/acm/contest/393/C 这个题就是对于每个数算贡献,如果有相同的数,只计算先出现的数的贡献 对于数x,若它在前i行的数目分别为a1 ...

  5. 牛客练习赛42D(性质、数学)

    题目传送 就像题解所说的,写几个可以发现有分成四段的性质:第一段是从n开始往下贪,第二段是个数字,第三段……卧槽好吧真难描述. 然后发现这个数据量可达1e9,所以考虑“二分确定序列+数学计算”的方式解 ...

  6. 牛客练习赛42 C 反着计算贡献

    https://ac.nowcoder.com/acm/contest/393/C 题意 给你一个矩阵, 每次从每行挑选一个数,组成一个排列,排列的和为不重复数字之和,求所有排列的和(n,m<= ...

  7. 牛客练习赛42 A 字符串

    题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...

  8. 牛客练习赛42 E.热爆了

    这可能是全场最长的一份代码 问的其实是对于关键点的斯坦纳树大小 考虑补集转化,不合法的点就是它的子树中没有关键点的点和斯坦纳树根的祖先 树根不难求,关键点中dfs序最大最小点的LCA就是了 问题在前者 ...

  9. 牛客练习赛1 矩阵 字符串二维hash+二分

    题目 https://ac.nowcoder.com/acm/contest/2?&headNav=www#question 解析 我们对矩阵进行二维hash,所以每个子矩阵都有一个额hash ...

随机推荐

  1. 【view】 视图组件说明

    view 是视图容器,可用于包裹其它组件或文本内容. 原型: <view hover-class="[String]" hover-stop-propagation=&quo ...

  2. 【if控制器】-(某种情况成立就执行的场景)

    if 控制器   一般来判断某种特殊情况 成立,就执行. JEXL Expression to evaluate:此处直接填写需要进行判断的表达式即可 表达式支持: ==  是否等于,如${__jex ...

  3. python邮件服务-yagmail

      下载安装 yagmail import yagmail #链接邮箱服务器 #此处的password是授权码 yag= yagmail.SMTP( user="843092012@qq.c ...

  4. Python3 迭代器,生成器,装饰器

    1.迭代器 迭代器有两个基本方法,iter()和next(),next()完成后会引发StopIteration异常 a='abcdef' b=iter(a) #创建迭代器对象 print(type( ...

  5. codeforces 301D Yaroslav and Divisors(树状数组)

    Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he ...

  6. ZOJ 2760 How Many Shortest Path(最短路径+最大流)

    Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...

  7. 【转】MySQLroot用户忘记密码解决方案(安全模式,修改密码的三种方式)

    文章出自:http://www.2cto.com/database/201412/358128.html 1.关闭正在运行的MySQL2.启动MySQL的安全模式,命令如下: ? 1 mysqld - ...

  8. fragment的介绍与使用

    稍稍摘录一段Fragment.java中的说明文档. /** * A Fragment is a piece of an application's user interface or behavio ...

  9. str和repr

    在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...

  10. Android中Parcelabel对象的使用和理解

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...