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$ 个 ...
随机推荐
- sersync之不洗澡
inotiry图片参考 sersync图片参考 inotify文字教程 该软件对系统有要求,内核2.6以上,并且有如下目录,后面会讲解三个文件用途 [root@jokerpro ~]# uname - ...
- socket编程之select()
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 参数 ...
- Ajax的基础应用
什么是ajax Ajax: asynchronous javascript and xml (异步js和xml) 其是可以与服务器进行(异步/同步)交互的技术之一. ajax的语言载体是java ...
- python mac下使用多进程报错解决办法
使用pychram运行python web,web使用了多进程 mac下运行会提示如下: may have been in progress in another thread when fork() ...
- R语言可视化学习笔记之添加p-value和显著性标记
R语言可视化学习笔记之添加p-value和显著性标记 http://www.jianshu.com/p/b7274afff14f?from=timeline 上篇文章中提了一下如何通过ggpubr ...
- vuex-- Vue.的中心化状态管理方案(vue 组件之间的通信简化机制)
vuex-- Vue.的中心化状态管理方案(vue 组件之间的通信简化机制) 如果你在使用 vue.js , 那么我想你可能会对 vue 组件之间的通信感到崩溃 .vuex就是为了解决组件通信问题的. ...
- leetcode687
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 转载----我与CMDB不得不说的故事
每次读到配置管理相关的书籍时,我总在想:“这些定义很精准,流程也很完整,但这不是真正的难题.”对于一个配置管理者来说,真正的难题不是绘制“庞大而精美”的数据模型,不是设计“全天候.无死角”的管控流程, ...
- linux下使用adb查看android手机的logcat
root@ubuntu:/home/song# adb logcat -s VLC
- win7下IIS的安装和配置图文教程
1. 首先是安装IIS.打开控制面板,找到”程序与功能”,点进去 2. 点击左侧”打开或关闭Windows功能” 3. 找到”Internet 信息服务”,按照下图打勾即可 等待安装完成 4. 安装完 ...