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. bzoj千题计划204:bzoj2813: 奇妙的Fibonacci

    http://www.lydsy.com/JudgeOnline/problem.php?id=2813 若j能整除i,则f[j]能整除f[i] 题目就变成了求约数个数和.约数的平方和 http:// ...

  2. 流媒体技术学习笔记之(三)Nginx-Rtmp-Module统计某频道在线观看流的客户数

    获得订阅者人数,可以方便地显示观看流的客户数. 查看已经安装好的模块 /usr/local/nginx/sbin/nginx -V 安装从源编译Nginx和Nginx-RTMP所需的工具 sudo a ...

  3. python中的__new__、__init__和__del__

    __new__.__init__.__del__三个方法用于实例的创建和销毁,在使用python的类中,我们最常用的是__init__方法,通常称为构造方法,__new__方法几乎不会使用,这篇文章是 ...

  4. Zookeeper笔记之使用zk实现集群选主

    一.需求 在主从结构的集群中,我们假设硬件机器是很脆弱的,随时可能会宕机,当master挂掉之后需要从slave中选出一个节点作为新的master,使用zookeeper可以很简单的实现集群选主功能. ...

  5. [Alg::Trick]小白鼠找毒酒

    题目来源:牛客网 https://www.nowcoder.com/questionTerminal/c26c4e43c77440ee9497b20118871bf1 8瓶酒一瓶有毒,用人测试.每次测 ...

  6. camera驱动框架分析(上)

    前言 camera驱动框架涉及到的知识点比较多,特别是camera本身的接口就有很多,有些是直接连接到soc的camif口上的,有些是通过usb接口导出的,如usb camera.我这里主要讨论前者, ...

  7. linux usb枚举过程分析之守护进程及其唤醒【转】

    转自:http://blog.csdn.net/xuelin273/article/details/38646765 usb热插拔,即usb设备可以实现即插即用,像U盘一样,插到电脑里就可以用,不用时 ...

  8. 激活Window和office工具

    激活Window和office工具:    第一种工具(已使用工具激活microsoft office professional plus 2013版本):         暴风激活工具(暴风激活工具 ...

  9. Ubuntu+Nginx+uWSGI+Flask应用

    Ubuntu 18.04,Nginx 1.14.0,uWSGI 2.0.17.1,Flask 1.0.2,Python 3.6.5, 多日未更新博客,就是在忙着把自己的Flask应用在Ubuntu上运 ...

  10. 输出联系变化的数字seq

    主要作用:输出联系变化的数字格式:Seq 分割符号 开始 间隔 结束开始默认是1,间隔默认是1,分隔符默认回车一位是结束,两位首末,三位首间隔末,没有四位,开始可以是负数主要参数:-f 指定格式打印- ...