其他分享
首页 > 其他分享> > NOI1999 钉子与小球 - 动态规划

NOI1999 钉子与小球 - 动态规划

作者:互联网

https://ac.nowcoder.com/acm/problem/16856


点击查看代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[52][52];
char a[52][52];
int n,m;
ll gcd(ll a,ll b){
    if(b==0)    return a;
    else    return gcd(b,a%b);
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<=i;j++){
            cin >> a[i][j];
        }
    }
    dp[0][0]=1;
    for(int i=0;i<n;i++){
        for(int j=0;j<=i;j++){
            if(a[i][j]=='*'){
                dp[i+1][j]+=dp[i][j];
                dp[i+1][j+1]+=dp[i][j];
            }
            else if(a[i][j]=='.'){
                dp[i+2][j+1]+=dp[i][j]*4;
            }
        }
    }
    ll sum=0;
    for(int j=0;j<=n;j++)    sum+=dp[n][j];
    ll g=gcd(dp[n][m],sum); 
    cout << dp[n][m]/g << "/" << sum/g << "\n"; 
    return 0;
}

标签:NOI1999,int,钉子,ll,小球,52,dp
来源: https://www.cnblogs.com/wujw11world/p/16580767.html