1. 程式人生 > >bzoj1867: [Noi1999]釘子和小球(DP)

bzoj1867: [Noi1999]釘子和小球(DP)

img pri include sca long long ont print close cnblogs

  一眼題...輸出分數格式才是這題的難點QAQ

  學習了分數結構體...

技術分享
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio> 
#include<algorithm>
#define ll long long 
using namespace std;
const int maxn=60,inf=1e9;
struct fra{ll u,d;fra(ll a=0,ll b=1){u=a,d=b;}}f[maxn][maxn];
int n,m; bool mp[maxn][maxn]; int readch() { char ch=getchar(); while(ch== ||ch==\n||ch==\t||ch==\r)ch=getchar(); return ch==*; } ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} void sim(fra &x){ll t=gcd(x.u,x.d);x.u/=t;x.d/=t;} fra operator+(fra x,fra y) { ll t=gcd(x.d,y.d); fra c
=fra(y.d/t*x.u+x.d/t*y.u,x.d/t*y.d); return sim(c),c; } fra operator*(fra x,fra y) { fra c=fra(x.u*y.u,x.d*y.d); return sim(c),c; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) mp[i][j]=readch(); f[1][1]=fra(1,1
); for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) if(mp[i][j])f[i+1][j]=f[i+1][j]+f[i][j]*fra(1,2),f[i+1][j+1]=f[i+1][j+1]+f[i][j]*fra(1,2); else f[i+2][j+1]=f[i+2][j+1]+f[i][j]; printf("%lld/%lld",f[n+1][m+1].u,f[n+1][m+1].d); return 0; }
View Code

bzoj1867: [Noi1999]釘子和小球(DP)