[sum_{i=1}^nfrac{1}{prod_{j=i}^{i+m-1}j}=S\
frac{0!}{m!}+frac{1!}{(m+1)!}+...+frac{(n-1)!}{(m+n-1)!}\
frac{0!frac{(m+n-1)!}{m!}+...(n-1)!frac{(m+n-1)!}{(m+n-1)!}}{(m+n-1)!}
]
先将分母分解质因数,最后giao精除以低精
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
inline int max(int a,int b){return a > b ? a : b;}
struct INT{
int num[2501],len = 0;
INT operator+(INT &x)const{
INT re;
memset(re.num,0,sizeof(re.num));
re.len = max(len,x.len);
for(int i = 1;i <= re.len;i++)
re.num[i] = num[i] + x.num[i];
int jw = 0;
for(int i = 1;i <= re.len;i++){
re.num[i] += jw;
jw = re.num[i]/10;
re.num[i] %= 10;
}
if(jw) re.num[++re.len] = jw;
return re;
}
INT operator*(int &x)const{
INT re;
memset(re.num,0,sizeof(re.num));
re.len = len;
for(int i = 1;i <= re.len;i++)
re.num[i] = num[i] * x;
int jw = 0;
for(int i = 1;i <= re.len;i++){
re.num[i] += jw;
jw = re.num[i]/10;
re.num[i] %= 10;
}
while(jw){
re.num[++re.len] = jw % 10;
jw /= 10;
}
return re;
}
bool operator/(const int &x){
INT re;
memset(re.num, 0, sizeof(re.num));
re.len = len;
int tw=0;
for(int i=len; i>=1; i--){
tw = tw * 10 + num[i];
re.num[i] = tw / x;
tw %= x;
}
if(tw) return false;//除不干净
while(!re.num[re.len]) re.len--;
if(!re.len) re.len++;
len = re.len;
for(int i=1; i<=re.len; i++)
num[i] = re.num[i];
return true;
}
INT prt(){
for(int i=len; i>=1; i--)
putchar(num[i]+'0');
putchar('
');
}
}fz,fm,fztemp;
vector<int> zys;//质因数
int n,m; bool isp[1005];
void shai(){
memset(isp,true,sizeof(isp));
isp[0] = isp[1] = false;
for(int i = 2;i <= 1000;i++)
if(isp[i]) for(int j = i+i;j <= 1000;j += i)
isp[j] = false;
}
inline void makezys(int x){
for(int i=2; i<=x; i++)
if(isp[i])
while(x%i==0){
zys.push_back(i);
x /= i;
}
}
int main(){
scanf("%d%d",&n,&m);
shai();
fztemp.len = fm.len = 1;
fztemp.num[1] = fm.num[1] = 1;
fz.len = 1;
fz.num[1] = 0;
for(int i=1; i<=n+m-1; i++)
makezys(i);
for(int i=m+1; i<=n+m-1; i++)
fztemp = fztemp * i;//先处理出0!*(n+m-1)!/m!
for(int i=0; i<=n-1; i++){
if(i) fztemp / (m+i);
if(i) fztemp = fztemp * i;
fz = fz + fztemp;
}
for(int i=0; i<zys.size(); i++){
if(fz/zys[i]) ;
else fm = fm*zys[i];//不可约分
}
fz.prt();
fm.prt();
return 0;
}