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. 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- ...

  2. bzoj千题计划191:bzoj2337: [HNOI2011]XOR和路径

    http://www.lydsy.com/JudgeOnline/problem.php?id=2337 概率不能异或 但根据期望的线性,可以计算出每一位为1的概率,再累积他们的期望 枚举每一位i,现 ...

  3. Git 操作指南

    http://blog.csdn.net/troy__/article/details/40082657

  4. 第9月第30天 MVP

    1. import UIKit struct Person { // Model let firstName: String let lastName: String } protocol Greet ...

  5. [Alg] 尺取法

    尺取法是在线性结构中进行搜寻满足某一条件的区间的方法. 该方法保存两个索引--首索引begin.尾索引end.判断 [begin, end] 区间是否满足条件. 移动 [begin, end] 区间的 ...

  6. 使用sp_executesql

    建议您在执行字符串时,使用 sp_executesql 存储过程而不要使用 EXECUTE 语句.由于此存储过程支持参数替换,因此 sp_executesql 比 EXECUTE 的功能更多:由于 S ...

  7. 浅谈tomcat中间件的优化【转】

    今天来总结一下tomcat的一些优化的方案,由于本人才疏学浅,写的不好,勿喷! tomcat对于大多数从事开发工作的童鞋应该不会很陌生,通常做为默认的开发环境来为大家服务,不过tomcat默认的一些配 ...

  8. 我们在部署 HTTPS 网站时,该如何选择SSL证书?

    我们在部署 HTTPS 网站时,该如何选择SSL证书? 首次部署HTTPS网站的同学对选择什么样的SSL证书多多少少都有点迷茫. 这里考虑的因素确实不少:是否支持多域名.泛域名,价格,信息泄露的保额, ...

  9. delete/truncate/drop table的区别以及锁在这里的角色

    数据库删除语句 Drop/Delete/Truncate比较 Delete :删除数据表中的行(可以删除某一行,也可以在不删除数据表的情况下删除所有行). 删除某一行:Delete from 数据表名 ...

  10. Java ListIterator(迭代器)

    LIstIterator是一个更加强大的Iterator的子类型,它只能用于各种List类的访问,尽管Iterator只能向前移动,但是ListIterator可以双向移动,它还可以产生相对于迭代器在 ...