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$ 个 ...
随机推荐
- HDU-3661-Assignments
/* Assignments Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- python学习笔记(八):异常处理
一.异常处理 在程序运行过程中,总会遇到各种各样的错误.程序一出错就停止运行了,那我们不能让程序停止运行吧,这时候就需要捕捉异常了,通过捕捉到的异常,我们再去做对应的处理. 下面我们先写一个函数,实现 ...
- [CSAPP] Chapter 1 Overview of Computer
1.1 information is bits + context All computer programs are just a sequence of bits, each with a val ...
- Android 4 学习(20):ActionBar
参考<Pro Android 4.0> ActionBar 11.0之后,ActionBar在Activity中默认存在,可以在代码中设置其显示与否: ActionBar actionBa ...
- MongoDB出现CPU飚高,如何强制停止正在执行的操作
如果发出了一个执行耗时很长的任务给MongoDB服务器,客户端强制终止会导致任务依然在服务器端执行. 这时MongoDB提供了查询和管理正在执行任务的方式. // db.currentOp() 获得当 ...
- GeoServer之sqlserver插件安装
GeoServer之sqlserver插件安装 GeoServer可以直接从sqlserver中获取数据,用来发布wms服务.但是其sqlserver连接插件并没有直接集成在Geoserver中,需要 ...
- Log4j编写
来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记 ...
- spring注解扫描组件注册
最近对单点系统进行微服务拆分,被各个springboot的组件注册搞得云里雾里的.(有的是通过springboot的自动配置进IOC容器的,有的是自己添加构造方法添加进IOC容器.)决定抽时间将spr ...
- redis centos7
官网下载tar包 make 修改conf 修改 启动脚本 utils/redis_init_script 开放端口6379
- php-yii-form标签
yii 标签用法(模板) (2013-08-14 17:28:19) 转载▼ 标签: it 分类: yii yii模板中的label标签 <?php echo $form->labelEx ...