Solution

$jzy$大佬用了给的原根的信息,加上矩阵快速幂150行QAQ

然而$yuli$大佬的做法不仅好懂,代码只有50行!

快速幂的思想,把m看成要组成的区间总长度,每次将两段组合得到新的长度。

定义$g[i]$表示当前x为$i$时的方案数,用来最后计算期望,在快速幂中相当于ans,定义$f[i]$代表a,是初始要用来组合的长度为1的方案,再用一个辅助数组转移即可。

Code

#include<bits/stdc++.h>
#define MOD 1000000007
#define LL long long
using namespace std; LL mpow(LL a, LL b) {
LL ans = ;
for(; b; b >>= , a = a * a % MOD)
if(b & ) ans = ans * a % MOD;
return ans;
} int n, m, mod, a;
LL f[], fz[], g[];
int main() {
freopen("rand.in", "r", stdin);
freopen("rand.out", "w", stdout);
scanf("%d%d%d", &n, &m, &mod);
for(int i = ; i <= n; i ++) scanf("%d", &a), f[a] ++;
g[] = ; int M = m;
while(m) {
if(m % ) {
for(int i = ; i < mod; i ++) fz[i] = ;
for(int i = ; i < mod; i ++)
for(int j = ; j < mod; j ++)
fz[i * j % mod] = (fz[i * j % mod] + 1ll * g[i] * f[j]) % MOD;
for(int i = ; i < mod; i ++) g[i] = fz[i];
}
for(int i = ; i < mod; i ++) fz[i] = ;
for(int i = ; i < mod; i ++)
for(int j = ; j < mod; j ++)
fz[i * j % mod] = (fz[i * j % mod] + 1ll * f[i] * f[j]) % MOD;
for(int i = ; i < mod; i ++) f[i] = fz[i];
m >>= ;
}
LL ans = ;
for(int i = ; i <= mod; i ++) ans = (ans + 1ll * g[i] * i) % MOD;
ans = ans * mpow(mpow(n, M), MOD - ) % MOD;
printf("%lld", ans);
return ;
}

Solution

完全没想到是带权并查集!!

网上大佬讲解的很好!写的时候细节也比较多,对带权并查集理解深了一层了QAQ想不通的一点就是排序如果按二维排就会错一个点??

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std; inline LL read() {
LL x = ; int t = ; char ch = getchar();
while(!isdigit(ch)) t |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * + ch - '', ch = getchar();
return x * (t ? - : );
} struct Node {
int x, y; LL w;
} a[];
bool cmp1(Node a, Node b) { return a.x < b.x; }
bool cmp2(Node a, Node b) { return a.y < b.y; } LL fx[], fy[], wx[], wy[];
int findx(int x) {
if(fx[x] == x) return fx[x];
int hx = findx(fx[x]);
wx[x] += wx[fx[x]];
return fx[x] = hx;
} bool checkx(int x, int y, LL w) {
int hx = findx(x), hy = findx(y);
if(hx == hy) return wx[x] == wx[y] + w;
fx[hx] = hy; wx[hx] = wx[y] + w - wx[x];
return ;
} int findy(int x) {
if(fy[x] == x) return fy[x];
int hy = findy(fy[x]);
wy[x] += wy[fy[x]];
return fy[x] = hy;
} bool checky(int x, int y, LL w) {
int hx = findy(x), hy = findy(y);
if(hx == hy) return wy[x] == wy[y] + w;
fy[hx] = hy; wy[hx] = wy[y] + w - wy[x];
return ;
} int R, C, n;
LL Min[], Max[];
bool work() {
R = read(), C = read();
n = read();
memset(Min, , sizeof(Min));
memset(Max, , sizeof(Max));
int p = ;
for(int i = ; i <= n; i ++) {
a[i].x = read(), a[i].y = read(), a[i].w = read();
if(a[i].w < || (R * C <= && a[i].w >= && R != )) p = ;
}
if(!p) return ;
for(int i = ; i <= R; i ++) fx[i] = i, wx[i] = ;
for(int i = ; i <= C; i ++) fy[i] = i, wy[i] = ;
sort(a + , a + + n, cmp1);
for(int i = ; i < n; i ++) {
if(a[i].x == a[i + ].x && !checky(a[i].y, a[i + ].y, a[i + ].w - a[i].w))
return ;
}
sort(a + , a + + n, cmp2);
for(int i = ; i < n; i ++) {
if(a[i].y == a[i + ].y && !checkx(a[i].x, a[i + ].x, a[i + ].w - a[i].w))
return ;
}
for(int i = ; i <= n; i ++) {
int x = findx(a[i].x);
Min[x] = min(Min[x], a[i].w + wx[a[i].x]);
}
for(int i = ; i <= R; i ++) {
int x = findx(i);
Max[x] = min(Max[x], -wx[i]);
}
for(int i = ; i <= R; i ++) {
if(Min[i] + Max[i] < && fx[i] == i) return ;
}
return ;
} int main() {
freopen("then.in", "r", stdin);
freopen("then.out", "w", stdout);
int T;
scanf("%d", &T);
while(T --) {
if(work()) puts("Yes");
else puts("No");
}
return ;
}

Solution

乍一看怎么那么像聪聪与可可??然而这实际上是面具下隐藏着的小模拟!!!QAQ

仔细读读题发现自己的移动根本是没有用的,因为这一步除了使距离拉近一步外什么都没有做,不如给自己回蓝或者尽量去打香港记者,因为此时的距离的贡献一定比以后优。

然后就是能打就打,不能打就回蓝。

香港记者走的永远是受伤害最小的地方,因此要比较三个方位,然而就是这里让100分打水漂了QAQ

因为在最后他也可以只走一步,不一定一定要走两步,所以下面的写法就很精髓了QAQ

for(int i = 1; i <= 2; i++) {
if(tx == 0 && ty == 0) break;
if(tx <= ty) ty--;
else tx--;
}

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std; inline int read() {
int x = ; int t = ; char ch = getchar();
while(!isdigit(ch)) t |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * + ch - '', ch = getchar();
return x * (t ? - : );
} int a, b;
int a1, a2, b1, b2, c, d; inline LL Min(LL a, LL b) {
return a < b ? a : b;
} void work() {
a1 = read(), b1 = read(), a2 = read(), b2 = read(), c = read(), d = read();
int tx = abs(a1 - a2), ty = abs(b1 - b2);
int flag = ;
while() {
if(d <= ) break;
if(tx == && ty == ) { flag = ; break; }
if(c < a) c += b;
else c -= a, d -= tx * tx + ty * ty;
if(d <= ) break;
for(int i = ; i <= ; i++) {
if(tx == && ty == ) break;
if(tx <= ty) ty--;
else tx--;
}
}
if(flag) printf("NAIVE\n");
else printf("SIMPLE\n");
} int main() {
freopen("do.in", "r", stdin);
freopen("do.out", "w", stdout);
int T;
T = read();
a = read(), b = read();
while(T --) {
work();
}
return ;
}

【11.1校内测试】【快速幂DP】【带权并查集】【模拟】的更多相关文章

  1. POJ1417:True Liars(DP+带权并查集)

    True Liars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

  3. luogu 2294 狡猾的商人 带权并查集

    此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...

  4. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  5. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  6. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  7. POJ 1733 Parity game (带权并查集)

    题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...

  8. POJ1984:Navigation Nightmare(带权并查集)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7871   Accepted: 2 ...

  9. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

随机推荐

  1. 介绍C++11标准的变长参数模板

    目前大部分主流编译器的最新版本均支持了C++11标准(官方名为ISO/IEC14882:2011)大部分的语法特性,其中比较难理解的新语法特性可能要属变长参数模板(variadic template) ...

  2. Spark记录-源码编译spark2.2.0(结合Hive on Spark/Hive on MR2/Spark on Yarn)

    #spark2.2.0源码编译 #组件:mvn-3.3.9 jdk-1.8 #wget http://mirror.bit.edu.cn/apache/spark/spark-2.2.0/spark- ...

  3. Spark记录-Scala数组

    Scala提供了一种数据结构叫作数组,数组是一种存储了相同类型元素的固定大小顺序集合.数组用于存储数据集合,但将数组视为相同类型变量的集合通常更为有用. 可以声明一个数组变量,例如:numbers,使 ...

  4. sssss

    关于征集参加第五届世界互联网大会“世界互联网领先科技成果发布活动”相关成果的通知 2018年07月24日 08:55:00来源: 中国网信网     [打印] [纠错]     各有关单位/个人: 第 ...

  5. [软件]在浏览器里添加MarkDown Here(插件)

    1. 先来说说这个插件的作用是什么: 用于在网页一些编辑文本的地方, 使用MacDown编辑文本 支持大部分浏览器,  https://github.com/adam-p/markdown-here ...

  6. Linux下sh文件运行及桌面环境双击运行sh文件

    sh文件运行: 1.修改为可执行权限: chmod u+x hello.sh 2.运行 ./hello.sh 3.不使用可执行权限修改,用sh直接运行 sh ./hello.sh 桌面环境双击运行sh ...

  7. Django Book 学习笔记(上)

    拜读了网上的Django Book,现在来总结一下吧...... 一.Django的配置 非常的蛋疼,由于Django的块组之间耦合度低,这既是它的优点,也是它的缺点.我在Ubuntu所配置的Djan ...

  8. 利用Python生成随机密码

    #coding:utf-8 #利用python生成一个随机10位的字符串 import string import random import re list = list(string.lowerc ...

  9. 如何新建Quartus工程—FPGA入门教程【钛白Logic】

    这一章我们来实现第一个FPGA工程—LED流水灯.我们将通过流水灯例程向大家介绍一次完整的FPGA开发流程,从新建工程,代码设计,综合实现,管脚约束,下载FPGA程序.掌握本章内容,大家就算正式的开始 ...

  10. CMD常用命令(一)

    常用命令大全 net>user heibai lovechina /add 加一个heibai的用户密码为lovechina net>localgroup Administrators h ...