CodeForces 630 I. Parking Lot(组合数学)

来源:互联网 发布:张宴 nginx 编辑:程序博客网 时间:2024/05/15 10:51

Description
一公司奖励员工4种不同的汽车,每种汽车完全相同,而公司的停车场只有2n-2个停车位,所以停车场停不下所有车,甚至连一种车都停不完,而老板觉得如果停车场恰有n辆相同的车排在一起会很好看,问有多少种停车方案
Input
一个整数n(3<=n<=30)
Output
停车方案数
Sample Input
3
Sample Output
24
Solution
简单组合,如果这n辆车在停车场两端,那么停车方案数为4 * 3 * 4 ^ (n-3),如果这n辆车不在两端,那么停车方案为4 * (n-3) * 3 * 3 * 4 ^ (n-4),故ans=24 * 4 ^ (n-3)+36 * (n-3) * 4 ^ (n-4)
Code

#include<cstdio>#include<iostream>using namespace std;typedef long long ll;int n;ll mod_pow(ll a,int b){    if(b<0)return 1;    ll ans=1ll;    while(b)    {        if(b&1)ans=ans*a;        a=a*a;        b>>=1;    }    return ans;}int main(){    while(~scanf("%d",&n))    {        ll ans=24*mod_pow(4ll,n-3)+36*(n-3)*mod_pow(4ll,n-4);        printf("%I64d\n",ans);    }    return 0;}
0 0
原创粉丝点击