[Contest20180328]coin
转化一下,相当于从$0$跳到$M$,每一步跳跃距离为$v_i$中的某个,每次跳跃距离不大于上一次,统计方案数
用$f_{i,j,k}$表示跳到$i$,第一步跳$v_j$,最后一步跳$\geq v_k$的方案数,那么有转移$f_{a+b,i,j}=\sum\limits_{k=1}^nf_{a,i,k}f_{b,k,j}$,跟矩阵乘法一模一样,所以我们可以把每个$f_i$看成矩阵,用矩阵乘法转移
先预处理出$f_{v_{1\cdots n}}$,显然$f_{v_1,1,1}=1,f_{v_1,i,j}=0$,然后有$f_{v_i}=f_{v_{i-1}}^{\frac{v_i}{v_{i-1}}}+A_i$(其中$A_i$的第$i$行的前$i$位是$1$,其它位置是$0$)因为$v_i$是$v_{i-1}$的倍数,再加上从起点一步跳到$v_i$这种方法,所以有这样的转移
最后来统计答案,也就是计算$f_M$,直接从$v_n$到$v_1$贪心地取转移就好了
为什么?假如有这样一种贪心跳法:一直跳$v_n$直到不能跳,然后跳$v_{n-1}$,以此类推,假设它经过的点叫做“关键点”,那么关键点一定被所有不同的跳法所经过,因为这些跳法都是把贪心跳法中的大步换成小步得到的($v_i$中两两互为倍数或因数),这样就说明了以上计算$f_M$的方法是正确的
#include<stdio.h> #include<string.h> const int mod=998244353; typedef long long ll; int mul(int a,int b){return a*(ll)b%mod;} int ad(int a,int b){return(a+b)%mod;} int n; struct matrix{ int a[51][51]; matrix(){memset(a,0,sizeof(a));} }t[51],ans; matrix operator*(matrix a,matrix b){ int i,j,k; matrix c; for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ for(k=1;k<=n;k++)c.a[i][j]=ad(c.a[i][j],mul(a.a[i][k],b.a[k][j])); } } return c; } matrix pow(matrix a,ll b){ matrix s; for(int i=1;i<=n;i++)s.a[i][i]=1; while(b){ if(b&1)s=s*a; a=a*a; b>>=1; } return s; } ll v[51]; int main(){ ll m,k; int i,j; scanf("%d%lld",&n,&m); for(i=1;i<=n;i++)scanf("%lld",v+i); t[1].a[1][1]=1; for(i=2;i<=n;i++){ k=v[i]/v[i-1]; t[i]=pow(t[i-1],k); for(j=1;j<=i;j++)t[i].a[i][j]++; } for(i=1;i<=n;i++)ans.a[i][i]=1; for(i=n;i>0;i--){ k=m/v[i]; m%=v[i]; ans=ans*pow(t[i],k); } k=0; for(i=1;i<=n;i++)k=ad(k,ans.a[i][1]); printf("%lld",k); }
[Contest20180328]coin的更多相关文章
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- [luogu2964][USACO09NOV][硬币的游戏A Coin Game] (博弈+动态规划)
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- LeetCode Coin Change
原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...
- ACM Coin Test
Coin Test 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 As is known to all,if you throw a coin up and let ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- leetcode:Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- UVa 674 Coin Change【记忆化搜索】
题意:给出1,5,10,25,50五种硬币,再给出n,问有多少种不同的方案能够凑齐n 自己写的时候写出来方案数老是更少(用的一维的) 后来搜题解发现,要用二维的来写 http://blog.csdn. ...
- Epic - Coin Change
Something cost $10.25 and the customer pays with a $20 bill, the program will print out the most eff ...
随机推荐
- 2018 BAT最新 php面试必考题
收集一些实用php面试题及答案给大家 做为程序员,到IT企业面试的时候肯定会有笔试这关,那就要考考你的PHP知识了,所以本站收集一些实用的php面试题及答案给大家. 基础题: 1.表单中 get与 ...
- last_query_cost
The total cost of the last compiled query as computed by the query optimizer. This is useful for com ...
- URAL - 1486 Equal Squares 二维哈希+二分
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...
- (转)Notepad++
包括notepad++的详细的安装过程,插件使用说明,技巧…… crifan http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/ ...
- c#之字符串函数
1.常用的字符串函数 Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 int Compare(string str1,string str2) int Compare(s ...
- shell分发文件脚本
配置文件scp.conf ssh_hosts=("IP") #需要分发机器的所有IP ssh_ports=("22") ssh_users=("roo ...
- bzoj3127/3697 [Usaco2013 Open]Yin and Yang
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3127 http://www.lydsy.com/JudgeOnline/problem.ph ...
- JAVA开发环境及其开发
成功安装之后,进行测试是否真的成功安装,点击[开始]----[运行]----输入 CMD,在命令提示符里面输入"Java -version"并按回车键,出现下图,即为安装成功. 选 ...
- TCP/IP Http的区别
TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据. 关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以 ...
- Jmeter4.0启动闪退问题解决方案
jmeter:4.0 jdk版本:1.8 在Jmeter.bat的最后添加pause可以让Jmeter启动停止: 添加了pause进行强制停止在启动命令页面,查看到Jmeter报错信息如下: 第一次解 ...