ZOJ 3380 Patchouli's Spell Cards
生活随笔
收集整理的這篇文章主要介紹了
ZOJ 3380 Patchouli's Spell Cards
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方案數,$dp$。
總的方案數有$n^m$種,符合要求的直接算不好算,可以算反面,即不符合要求的。
設$dp[i][j]$表示前$i$種等級填了$j$個位置,那么$dp[i][j]=sum(dp[i-1][j-k]*c[m-(j-k)][k])$。初始化$dp[0][0]=1$。
符合要求的方案數為$n^m-dp[n][m]$。數字會爆$long$ $long$,上$java$。
import java.math.BigInteger; import java.util.Scanner;public class Main {public static BigInteger GCD(BigInteger a,BigInteger b){if(b.equals(BigInteger.ZERO)) return a;return GCD(b,a.mod(b));}public static void main(String [] args){Scanner cin = new Scanner(System.in);int M,N,L;BigInteger c[][] = new BigInteger[105][105];BigInteger dp[][] = new BigInteger[105][105];for(int i=0;i<=100;i++) c[i][0] = c[i][i] = BigInteger.ONE;for(int i=1;i<=100;i++){for(int j=1;j<i;j++){c[i][j]= c[i-1][j-1].add(c[i-1][j]);}for(int j=i+1;j<=100;j++) c[i][j] = BigInteger.ZERO;}while(cin.hasNext()){M=cin.nextInt();N=cin.nextInt();L=cin.nextInt();if(L>M) System.out.println("mukyu~");else {BigInteger n = BigInteger.valueOf(N);BigInteger fm = n.pow(M);BigInteger fz = new BigInteger("0");for(int i=0;i<=N;i++){for(int j=0;j<=M;j++){dp[i][j] = BigInteger.ZERO;}}dp[0][0] = BigInteger.ONE;for(int i=1;i<=N;i++){for(int j=0;j<=M;j++){for(int k=0;k<=L-1;k++){if(j-k<0) continue;if(M-(j-k)<0) continue;dp[i][j] = dp[i][j].add(dp[i-1][j-k].multiply(c[M-(j-k)][k]));}}}fz=dp[N][M]; fz = fm.subtract(fz);BigInteger gcd = GCD(fz,fm);fz=fz.divide(gcd); fm=fm.divide(gcd);System.out.println(fz+"/"+fm);}}} }?
轉載于:https://www.cnblogs.com/zufezzt/p/6337372.html
總結
以上是生活随笔為你收集整理的ZOJ 3380 Patchouli's Spell Cards的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【历史上的今天】3 月 12 日:万维网
- 下一篇: [Pytorch系列-30]:神经网络基