124 - ZOJ Monthly, March 2013 - E Choosing number 选数的方法数
来源:互联网 发布:mysql 表 字段关联 编辑:程序博客网 时间:2024/06/11 20:09
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=4963
There are n people standing in a row. And There are m numbers, 1.2...m. Every one should choose a number. But if two persons standing adjacent to each other choose the same number, the number shouldn't equal or less thank. Apart from this rule, there are no more limiting conditions.
And you need to calculate how many ways they can choose the numbers obeying the rule.
Input
There are multiple test cases. Each case contain a line, containing three integer n (2 ≤ n ≤ 108), m (2 ≤ m ≤ 30000), k(0 ≤ k ≤ m).
Output
One line for each case. The number of ways module 1000000007.
Sample Input
4 4 1
Sample Output
216
Author: GU, Shenlong
题意: 输入 n m k
有n个人 选择m个数 每个人可以任意选数 , 当2个相邻的人选择相同的数 当且仅当这个数大于k 问总共有多少种方法选择数
思路:
设x0 为上一个人选的时候选择的小于k的数的方法数, x1为上一个人选的时候选择的大于k的数的方法数
y0为当前人选择的小于k的数的方法数,y1为当前人选择的大于k的数的方法数
则有 y0=x0*(k-1)+x1*k
y1=x0*(m-k)+x1*(m-k)
这样可以构造一个矩阵了
; k-1 k ; ;x0; ;y0
; ; * ; ; = ;
; m-k m-k ; ;x1; ;
#include <stdio.h>#define N 2#define mod 1000000007struct mat{long long mar[2][2];};mat a,b,c,init,temp;mat res= { 1,0, 0,1 };mat mul(mat a1,mat b1) { long long i,j,l; mat c1; for (i=0;i<N;i++) { for (j=0;j<N;j++) { c1.mar[i][j]=0; for (l=0;l<N;l++) { c1.mar[i][j]+=(a1.mar[i][l]*b1.mar[l][j])%mod; c1.mar[i][j]%=mod; } } } return c1; } mat er_fun(mat e,long long x){mat tp;tp=e;e=res;while(x){if(x&1)e=mul(e,tp);tp=mul(tp,tp);x>>=1;}return e;}long long mi(long long a,long long k,long long M){ long long b=1; while(k>=1){ if(k%2==1){ b=a*b%M; } a=(a%M)*(a%M)%M; k/=2; } return b;}int main(){long long i,j;long long x1,x2;long long dp[2];long long ss;long long n,m,k;while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF){if(k!=0){init.mar[0][0]=k-1;init.mar[0][1]=k;init.mar[1][0]=m-k;init.mar[1][1]=m-k;a=init; b=er_fun(a,n-1);dp[0]=k;dp[1]=m-k;ss=b.mar[0][0]*dp[0]%mod+b.mar[0][1]*dp[1]%mod+b.mar[1][0]*dp[0]%mod+b.mar[1][1]*dp[1]%mod;ss=ss%mod;printf("%lld\n",ss);}else{printf("%lld\n",mi(m,n,1000000007));}}return 0;}
- 124 - ZOJ Monthly, March 2013 - E Choosing number 选数的方法数
- ZOJ Monthly March 2013 E & H
- ZOJ Monthly, March 2013 解题报告
- ZOJ Monthly, March 2014
- XMU1460 最高得分 &&124 - ZOJ Monthly, March 2013 - D 01背包巧妙处理
- hdu 1394 Minimum Inversion Number || ZOJ Monthly, January 2003 || 线段树 + 逆序数
- zoj 3690 Choosing number
- ZOJ 3690 Choosing number
- ZOJ 3690 Choosing number
- ZOJ 3622 Magic Number(数)
- 浙大月赛 ZOJ Monthly, March 2014(简单题的题解)
- ZOJ Monthly, March 2014,3765 Lights (Splay 基本操作,并维护区间上的信息 * 模板)
- ZOJ 3762 - ZOJ Monthly, March 2014 几何(0304修正)
- ZOJ 3764 - ZOJ Monthly, March 2014 最大流最小割
- ZOJ 3759 - ZOJ Monthly, March 2014 pell方程求解
- ZOJ Monthly, March 2013 A题 A Simple Tree Problem(线段树)#zh
- ZOJ Monthly, March 2014 (2014省赛练习)
- 矩阵快速幂 zoj-3690 Choosing number
- expect脚本自动登录
- [gif]GIF图形文件格式文档
- 快捷B2C水果超市——B2C红海中的一条生路
- OGNL表达式使用
- 配置bugzilla
- 124 - ZOJ Monthly, March 2013 - E Choosing number 选数的方法数
- UIView设置成圆角
- IOS开发之UIKeyboardType类型
- 测试用例管理系统需求细则
- Jquery.Sorttable 桌面拖拽自定义
- hdu 4527 小明系列故事——玩转十滴水
- softmax
- linux下镜像文件扩容的方法
- EJB3.0中拦截器的实现