【11.1校内测试】【快速幂DP】【带权并查集】【模拟】
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】【带权并查集】【模拟】的更多相关文章
- POJ1417:True Liars(DP+带权并查集)
True Liars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj1417(带权并查集+背包DP+路径回溯)
题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...
- luogu 2294 狡猾的商人 带权并查集
此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- POJ 1733 Parity game (带权并查集)
题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- 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 ...
随机推荐
- 在CentOS上导出JVM内存信息
首先看下Tomcat的进程Id: [root@iZ25Z ~]# ps aux | grep java www 2111 4.0 23.5 1637648 452756 ? Sl 10:12 4:35 ...
- OpenResty 扩展库(二)lua-resty-template
Lua和OpenResty的模板引擎(HTML) 模板语法 您可以在模板中使用以下标签: {{expression}},写入表达式的结果 - html转义 {*expression*},写入表达结果 ...
- Spring 学习01
一.Spring概念 1 spring是开源的轻量级框架 2 spring核心主要两部分: (1)aop:面向切面编程,扩展功能不是修改源代码实现 (2)ioc:控制反转, - 比如有一个类,在类里面 ...
- 安装mongodb以及设置为windows服务 详细步骤
我的win7 32的,注意版本要正确! 一.下载mongodb压缩包:mongodb-win32-i386-2.6.9.zip() 二.在D盘新建文件夹mongodb,将压缩我的解压文件放进去(有一个 ...
- java Runnable、Callable、FutureTask 和线程池
一:Runnable.Callable.FutureTask简介 (1)Runnable:其中的run()方法没有返回值. ①.Runnable对象可以直接扔给Thread创建线程实例,并且创建的线程 ...
- Java并发——线程同步Volatile与Synchronized详解
0. 前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52370068 面试时很可能遇到这样一个问题:使用volatile修饰in ...
- Linux下JDK到底应该安装在哪儿?
1 Linux 目录结构 即使这是个菜鸟级的问题,也经常难住老鸟.我就见过很资深的程序员把JDK不合适地安装到/home目录下.虽然不一定有最正确的安装位置,但一定有不适当的安装位置.为了确定我们到底 ...
- Redis常见操作命令
1.库相关 select 索引 => 选择库 dbsize => 查询当前库中Key的数量 flushdb => 清空当前库 flushall => 清空所有库(建议不要用,除 ...
- 直接读取修改exe文件
1. 前言 配置器的编写有很多的方式,主要是直接修改原始的受控端的程序,有的方式是把受控端和配置信息都放到控制端程序的内部,在需要配置受控端的时候直接输入配置信息,生成受控端:也有的方式是在外部直接修 ...
- click模块使用
# coding:utf8 import time import click @click.command() @click.option('--id', default='123', help='a ...