CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)
题意:给定 n 和k,n 表示有n个房子,然后每个有一个编号,一只鹅要从一个房间中开始走,下一站就是房间的编号,现在要你求出有多少种方法编号并满足下面的要求:
1.如果从1-k房间开始走,一定能直到 1。
2.如果从k+1到n 开始走,一定走不到 1.
3.如果从 1 开始走,那么一定能回到1,并且走过房间数不为0.
析:这个题,当时想了好久,其实并不难,当时是暴力过的,一看 k 最大才是8,那么应该不会TLE,然后就暴力了。首先这前 k 个数和后面的数,完全没有关系,
后面就是 n-k的 n-k次方,关键是前面,我们可以一个一个的试,并判断会不会成立,用DFS即可。
这个题可以用快速幂来做。
代码如下:
#include <bits/stdc++.h> using namespace std;
typedef long long LL;
const int maxn = 1000000 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
int cnt, k;
int a[100];
set<int> ss; bool judge(int u, int rt){
if(a[u] == 1) return true;
if(a[u] != rt && u != a[u] && a[u] && !ss.count(u)){ ss.insert(u); return judge(a[u], rt); }
return false;
} void dfs(int cur){
if(cur == k+1){
for(int i = 2; i <= k; ++i){
ss.clear();
if(!judge(i, i)) return ;
}
++cnt;
return ;
} for(int i = 1; i <= k;++i){
a[cur] = i;
dfs(cur+1);
}
return ;
} int qickpow(LL a, LL b){
LL k = a;
LL ans = 1;
while(b){
if(b & 1){
ans = (ans * k) % mod;
} k = (k * k) % mod;
b >>= 1;
}
return (int) ans;
} int main(){
int ans;
int n;
scanf("%d %d", &n, &k);
int t = n - k;
ans = qickpow(n-k, n-k);
cnt = 0;
dfs(2);
ans = (ans * k * cnt) % mod;
cout << ans << endl;
return 0;
}
方法二:
#include <bits/stdc++.h> using namespace std;
typedef long long LL;
const int maxn = 1000000 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
int cnt, k; int qickpow(LL a, LL b){
LL k = a;
LL ans = 1;
while(b){
if(b & 1){
ans = (ans * k) % mod;
} k = (k * k) % mod;
b >>= 1;
}
return (int) ans;
} int main(){
int ans;
int n;
scanf("%d %d", &n, &k);
int t = n - k;
ans = qickpow(n-k, n-k);
cnt = 0;
LL ans1 = qickpow(k, k-1);
ans = (ans * ans1) % mod;
cout << ans << endl;
return 0;
}
CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)的更多相关文章
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)
题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...
- Codeforces 288E - Polo the Penguin and Lucky Numbers(数位 dp+推式子)
题目传送门 似乎我的解法和官方题解不太一样 纪念自己独立做出来的一道难度 2800 的题. 我们记 \(ans(x)\) 为 \([444...44,x]\) 的答案,显然答案为 \(ans(r)-a ...
- codeforces B. Polo the Penguin and Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...
- CodeForces 288A Polo the Penguin and Strings (水题)
题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...
- CodeForces 289B Polo the Penguin and Matrix (数学,中位数)
题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...
- CodeForces 289A Polo the Penguin and Segments (水题)
题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...
- Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]
题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...
随机推荐
- 第四章.使用ant编译hadoop eclipse插件
从hadoop 0.20.203以后,hadoop的发布包里,不再对eclipse插件进行jar包发布,而是给出了打包的代码,需要各位开发人员自己进行打包和设置.我们打的包必须跟自己使用的hadoop ...
- GTID 跳过脚本
跳过单个error STOP SLAVE; SET gtid_next = '3b977b7e-ed28-11e7-a8ff-b4969113b678:138609841'; BEGIN;COMMIT ...
- Mybatis扩展
分页插件PageHelper 其实Mybstis内部有实现逻辑分页的功能,但是较为麻烦和难用.这里记录一个分页插件PageHelper的使用,我们可以在它的github地址https://github ...
- 【学习笔记】Manacher
扔板子跑路 代码 POJ3974 #include <cstdio> #include <cstring> #include <algorithm> using n ...
- UVA-11292Dragon of Loowater
/* The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into ...
- javascript slice array to num subarray
var data = ['法国','澳大利亚','智利','新西兰','西班牙','加拿大','阿根廷','美国','0','国产','波多黎各','英国','比利时','德国','意大利','意大利 ...
- wkhtmktopdf
1: sudo apt-get install wkhtmltopdf 2: sudo apt-get xvfb 3: 切换到root用户: echo 'xvfb-run --server-a ...
- C++对象数组初始化
类对象 数组 初始化可以使用构造函数初始化,同时类有不同的构造函数,可以对类对象数组元素使用不同的构造函数;
- How To Install MongoDB on CentOS 6
How To Install MongoDB on CentOS 6 Posted on January 21, 2014 by J. Mays | Updated: January 22, 2014 ...
- 来谈谈 WebAssembly 是个啥?为何说它会影响每一个 Web 开发者?
作者:link 原文:What is WebAssembly and why it affects web developers! 你听说过WebAssembly吗?这是由Google, Micros ...