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$ 个 ...
随机推荐
- node的close
在http.ServerResponse对象的end方法被调用之前,如果连接被中断,将触发http.ServerResponse对象的close事件. var http=require("h ...
- VRF实例说明
Virtual Routing Forwarding VPN路由转发表,也称VPN-instance(VPN实例),是PE为直接相连的site建立并维护的一个专门实体,每个site在PE上 ...
- linux grep日志查询
ll access.2018-09-*.gz zcat access.2018-09-*.gz |grep --color '1073011900' | head -n 100 匹配字符由于管道h ...
- SVN 提交、更新、解决冲突等操作步骤
1. 纳入版本控制 ①新建文件abc.txt ②在文件上点右键 ③添加后文件图标发生变化 2. 提交 ①使用TortoiseSVN可以提交具体某一个文件,或某一个目录下的所有改变.方法就是在想要提交的 ...
- C#利用QrCode.Net生成二维码(Qr码
http://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html 现在网上很多应用都是用二维码来分享网址或者其它的信息.尤其在移动领域,二 ...
- leetcode680
class Solution { public: bool validPalindrome(string s) { int len = s.length(); ) return true; , len ...
- leetcode495
public class Solution { public int FindPoisonedDuration(int[] timeSeries, int duration) { ) { ; } ) ...
- 「小程序JAVA实战」swagger2的使用与接口测试(34)
转自:https://idig8.com/2018/08/31/xiaochengxujavashizhanswagger2deshiyongyujiekouceshi34/ 我们已经开发完了一个用户 ...
- 转载----开发运维资产管理系统cmdb一些观点扯淡
在新公司负责全网的自动化运维平台及给各个业务线提供接口数据.这工作和以前做的很类似,也算是比较顺手的工作,这段时候遇见一些问题,导致开发的前进速度的放慢了,具体有哪些的不完善,我这里就先不摆出了,但是 ...
- tornado 自定义session (二)
有了上一步的基础,我们将session改造成一个模块,这样我们就可以通过配置,来使用不同方式(redis,数据库等)的session. 添加一个新目录:TornadoSession conf.py是配 ...