想状态和钉子的位置如何匹配想了半天。。。后来发现不是一样的吗$qwq$


思路:当然是$DP$啦

提交:>5次(以为无故$RE$,实则是先乘后除爆了$long\space long$)

题解:

若有钉子,左右各乘$\frac{1}{2}$转移,否则,向下两层直接转移。

对于分数,分别维护分子和分母,然后加起来的时候,记着一定要写成

up[i][j]=up[i][j]*(b/G)+a*(dn[i][j]/G);
dn[i][j]=dn[i][j]*(b/G);

而非

up[i][j]=up[i][j]*b/G+a*dn[i][j]/G;
dn[i][j]=dn[i][j]*b/G;

(好吧也是我傻$qwq$)

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define ull unsigned long long
#define ll long long
#define R register ll
#define pause (for(R i=1;i<=10000000000;++i))
#define In freopen("NOIPAK++.in","r",stdin)
#define Out freopen("out.out","w",stdout)
namespace Fread {
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
if(ch==EOF) return EOF; do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return (ch<=||ch>=);}
inline void gs(char* s) {
register char ch; while(isempty(ch=getchar()));
do *s++=ch; while(!isempty(ch=getchar()));
}
} using Fread::g; using Fread::gs;
namespace Luitaryi {
const int N=;
int n,m;
ll up[N][N],dn[N][N];
bool w[N][N];
inline ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
inline void add(int i,int j,ll a,ll b) {
R G=gcd(dn[i][j],b);
up[i][j]=up[i][j]*(b/G)+a*(dn[i][j]/G);
dn[i][j]=dn[i][j]*(b/G);
G=gcd(up[i][j],dn[i][j]);
if(G) up[i][j]/=G,dn[i][j]/=G;
}
inline void main() {
n=g(),m=g()+;
for(R i=;i<=n;++i) for(R j=;j<=i;++j) { register char ch;
while(ch=getchar(),ch!='*'&&ch!='.');
w[i][j]=(ch=='*');
up[i][j]=,dn[i][j]=;
} for(R i=;i<=n;++i) up[n+][i]=,dn[n+][i]=;
up[][]=dn[][]=;
for(R i=;i<=n;++i) for(R j=;j<=i;++j) {
R a=up[i][j],b=dn[i][j];
if(w[i][j]) {
if(a%==) a/=; else b*=;
add(i+,j,a,b),add(i+,j+,a,b);
} else add(i+,j+,a,b);
} printf("%lld/%lld",up[n+][m],dn[n+][m]); }
}
signed main() {
Luitaryi::main();
}

2019.07.17

05-17 18:16