CodeForces 177(div1)B
2016-07-25 20:32
393 查看
B. Polo the Penguin and Houses
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n.
Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n).
Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes
to the house whose number is written on the plaque of house x (that is, to house px),
then he goes to the house whose number is written on the plaque of house px (that
is, to house ppx),
and so on.
We know that:
When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1.
When the penguin starts walking from any house indexed from k + 1 to n,
inclusive, he definitely cannot walk to house number 1.
When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house.
You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7).
Input
The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ min(8, n))
— the number of the houses and the number k from the statement.
Output
In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
Examples
input
output
input
output
题意:一个村庄有n个城市,每个城市上有一个值p,到城市i上,然后就要走到城市pi上,并且重复这个过程,求满足(1,从城市1-k出发,总能回到1,;2,从城市k+1到n出发,绝对回不了n;3,从城市1出发后,经过任意步总能回到1)条件的pi的设置方法数。
思路:从题意可以知道,第二条件只需要后面的n-k个数字和前面的k个数字没关系就行了,也就是n-k的n-k次方。再就是前面的k个数,根据它的要求暴力一下就行了,最大是8
,把8个数都算出来,后来算出来其实就是k的k-1次方,所以直接一个矩阵快速幂就够了#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
ll quickmod(ll a,ll b)
{
ll ans = 1;
while(b)
{
if(b&1)
{
ans = (ans*a)%mod;
}
a = (a*a)%mod;
b >>= 1;
}
return ans%mod;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
ll ans = quickmod(k,k-1);
ll sum = quickmod(n-k,n-k);
ans = (ll(ans*sum))%mod;
printf("%I64d\n",ans);
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n.
Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n).
Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes
to the house whose number is written on the plaque of house x (that is, to house px),
then he goes to the house whose number is written on the plaque of house px (that
is, to house ppx),
and so on.
We know that:
When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1.
When the penguin starts walking from any house indexed from k + 1 to n,
inclusive, he definitely cannot walk to house number 1.
When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house.
You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7).
Input
The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ min(8, n))
— the number of the houses and the number k from the statement.
Output
In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
Examples
input
5 2
output
54
input
7 4
output
1728
题意:一个村庄有n个城市,每个城市上有一个值p,到城市i上,然后就要走到城市pi上,并且重复这个过程,求满足(1,从城市1-k出发,总能回到1,;2,从城市k+1到n出发,绝对回不了n;3,从城市1出发后,经过任意步总能回到1)条件的pi的设置方法数。
思路:从题意可以知道,第二条件只需要后面的n-k个数字和前面的k个数字没关系就行了,也就是n-k的n-k次方。再就是前面的k个数,根据它的要求暴力一下就行了,最大是8
,把8个数都算出来,后来算出来其实就是k的k-1次方,所以直接一个矩阵快速幂就够了#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
ll quickmod(ll a,ll b)
{
ll ans = 1;
while(b)
{
if(b&1)
{
ans = (ans*a)%mod;
}
a = (a*a)%mod;
b >>= 1;
}
return ans%mod;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
ll ans = quickmod(k,k-1);
ll sum = quickmod(n-k,n-k);
ans = (ll(ans*sum))%mod;
printf("%I64d\n",ans);
}
相关文章推荐
- android 代码实现控件之间的间距
- [Android]在代码里运行另一个程序的方法
- 肯特·贝克:改变人生的代码整理魔法
- 网页恶意代码的预防
- 高手写的Tracer-Flash代码调试类代码下载
- CSS代码缩写技巧
- 非主流Q-zOne代码代码搜集第1/2页
- CreateWeb.vbs 代码
- Lua中编译执行代码相关的函数详解
- 更有效率的css代码编写第1/3页
- 代码中到底应不应当写注释?
- SQL语言查询基础:连接查询 联合查询 代码
- 论坛头像随机变换代码
- 非常有用的9个PHP代码片段
- .NET 常用功能和代码小结
- C#实现压缩HTML代码的方法
- asp编程中常用的javascript辅助代码第1/2页
- 高质量PHP代码的50个实用技巧必备(下)
- ajax基本通用代码示例
- C#超实用代码段合集