题意:给定 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即可。

这个题可以用快速幂来做。

代码如下:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long LL;
  5. const int maxn = 1000000 + 5;
  6. const int INF = 0x3f3f3f3f;
  7. const int mod = 1000000007;
  8. const int dr[] = {0, 0, 1, -1};
  9. const int dc[] = {1, -1, 0, 0};
  10. int cnt, k;
  11. int a[100];
  12. set<int> ss;
  13.  
  14. bool judge(int u, int rt){
  15. if(a[u] == 1) return true;
  16. if(a[u] != rt && u != a[u] && a[u] && !ss.count(u)){ ss.insert(u); return judge(a[u], rt); }
  17. return false;
  18. }
  19.  
  20. void dfs(int cur){
  21. if(cur == k+1){
  22. for(int i = 2; i <= k; ++i){
  23. ss.clear();
  24. if(!judge(i, i)) return ;
  25. }
  26. ++cnt;
  27. return ;
  28. }
  29.  
  30. for(int i = 1; i <= k;++i){
  31. a[cur] = i;
  32. dfs(cur+1);
  33. }
  34. return ;
  35. }
  36.  
  37. int qickpow(LL a, LL b){
  38. LL k = a;
  39. LL ans = 1;
  40. while(b){
  41. if(b & 1){
  42. ans = (ans * k) % mod;
  43. }
  44.  
  45. k = (k * k) % mod;
  46. b >>= 1;
  47. }
  48. return (int) ans;
  49. }
  50.  
  51. int main(){
  52. int ans;
  53. int n;
  54. scanf("%d %d", &n, &k);
  55. int t = n - k;
  56. ans = qickpow(n-k, n-k);
  57. cnt = 0;
  58. dfs(2);
  59. ans = (ans * k * cnt) % mod;
  60. cout << ans << endl;
  61. return 0;
  62. }

方法二:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long LL;
  5. const int maxn = 1000000 + 5;
  6. const int INF = 0x3f3f3f3f;
  7. const int mod = 1000000007;
  8. const int dr[] = {0, 0, 1, -1};
  9. const int dc[] = {1, -1, 0, 0};
  10. int cnt, k;
  11.  
  12. int qickpow(LL a, LL b){
  13. LL k = a;
  14. LL ans = 1;
  15. while(b){
  16. if(b & 1){
  17. ans = (ans * k) % mod;
  18. }
  19.  
  20. k = (k * k) % mod;
  21. b >>= 1;
  22. }
  23. return (int) ans;
  24. }
  25.  
  26. int main(){
  27. int ans;
  28. int n;
  29. scanf("%d %d", &n, &k);
  30. int t = n - k;
  31. ans = qickpow(n-k, n-k);
  32. cnt = 0;
  33. LL ans1 = qickpow(k, k-1);
  34. ans = (ans * ans1) % mod;
  35. cout << ans << endl;
  36. return 0;
  37. }

CodeForces 288B Polo the Penguin and Houses (暴力或都快速幂)的更多相关文章

  1. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  2. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  3. Codeforces 288E - Polo the Penguin and Lucky Numbers(数位 dp+推式子)

    题目传送门 似乎我的解法和官方题解不太一样 纪念自己独立做出来的一道难度 2800 的题. 我们记 \(ans(x)\) 为 \([444...44,x]\) 的答案,显然答案为 \(ans(r)-a ...

  4. codeforces B. Polo the Penguin and Matrix 解题报告

    题目链接:http://codeforces.com/problemset/problem/289/B 题目意思:给出一个 n 行 m 列的矩阵和数值 d .通过对矩阵里面的数进行 + d 或者 - ...

  5. CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)

    题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...

  6. CodeForces 288A Polo the Penguin and Strings (水题)

    题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...

  7. CodeForces 289B Polo the Penguin and Matrix (数学,中位数)

    题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...

  8. CodeForces 289A Polo the Penguin and Segments (水题)

    题意:给你 n 段区间,而且还是不相交的,然后你只能向左扩展左端点,或者向右扩展右端点,然后扩展最少的步数让整数总数能够整除 k. 析:很简单么,只要在记录算一下数量,然后再算出 k 的倍数差多少就行 ...

  9. Codeforces 785D - Anton and School - 2 - [范德蒙德恒等式][快速幂+逆元]

    题目链接:https://codeforces.com/problemset/problem/785/D 题解: 首先很好想的,如果我们预处理出每个 "(" 的左边还有 $x$ 个 ...

随机推荐

  1. 运维平台cmdb开发-day1

    序读项目由来 终极目标,运维平台.自动化.装机,监控,安装软件,部署基础服务,资产管理,之前是excel,现在是客户端自动获取,变更记录 后台管理 api 采集资产 四种模式agent 定时,每天执行 ...

  2. Julia - 字符串判断函数

    isascii() 判断是否是 ascii 码,返回 Bool 值 julia> isascii('a') true julia> isascii('α') false julia> ...

  3. [Z] Shell中脚本变量和函数变量的作用域

    在shell中定义函数可以使代码模块化,便于复用代码.不过脚本本身的变量和函数的变量的作用域问题可能令你费解,在这里梳理一下这个问题. (1)Shell脚本中定义的变量是global的,其作用域从被定 ...

  4. css伪类(Pseudo-classes)

    简介:伪类(Pseudo classes)是选择符的螺栓,用来指定一个或者与其相关的选择符的状态.它们的形式是selector:pseudo class { property: value; },简单 ...

  5. JavaScript知识总结--历史-html引用方式-基础概念

    一.JavaScript简介 1.ECMAScript 1995~今已经20年的历史,产生JavaScript是需要它去解决一定的问题:在浏览器端做一些数据的验证,试想当年的网络环境,如果能够在浏览器 ...

  6. 有关Zedgraph的功能扩展的笔记

    1.坐标轴范围.刻度调整后需要加上下面的语句才能刷新:     zedGraphControl1.AxisChange();     zedGraphControl1.Refresh(); 2.坐标范 ...

  7. winform中读取App.config中数据连接字符串

    1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...

  8. views获取数据 -- request包含的方法

    request.GET request.POST request.FILES request.path_info request.xxx.getlist request.method request. ...

  9. 安装sql server 2000

    昨天下午快下班的时候 因为公司需要折腾了下sql server 2000,先不说这么古老的版本,而且安装的也是醉了... 首先sql server 2000是基于32位的系统开发的,那时候据说还没有6 ...

  10. FreeSWITCH 客户端fs_cli连接设置(acl)

    FreeSWITCH 默认配置只能 在本机连接, 要从 外面连接, 就要配置: acl.conf.xml::network-lists/list event_socket.conf.xml::appl ...