您的位置:首页 > 其它

hdu 1133 Buy the Ticket (高精乘法,不需高精除法)

2015-08-04 23:50 369 查看
原题链接:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2§ionid=3&problemid=6

在看过网上的很多解答后,感觉大同小异。

我的做法:

1.不需要开二维数组。

2.不需要高精除法 (n!=0时,必定有m+1<=m+n)

3.不需要memcpy函数

分析:

1.n>m 很明显0种情况

2.直接求有多少正确的不同的队列数是比较难的。

所以利用 全部的情况-错误的情况 求得。

C(m+n,n)全部的情况,从m+n个位置选取n个位置为100元的。

C(m+n,m+1)错误的情况,从m+n个位置选取m+1个位置为100元的。

所以公式为(C(m+n,n)-C(m+n,m+1))*(m!)*(n!)

化简得:(m+n)!*( (m-n+1) / (m+1) )

代码如下:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int base = 10000;
int BigInt[100];
void BigMul(int b)
{
int  temp = 0;
for (int i = 99; i >= 0; i--)
{
temp += BigInt[i] * b ;
BigInt[i] = temp%base;
temp /= base;
}

}

void JC(int n, int fm)
{
for (int i = n; i >= 2; i--)
{
if (i!=fm)			//分母为m+1  n!=0时,必定有 m+1<=m+n   所以在相乘时直接约去。
BigMul(i);
}
}

int main()
{
int n, m, k = 0;
while (cin >> m >> n && (n || m))
{
memset(BigInt, 0, sizeof(BigInt));
BigInt[99] = 1;					//别忘了!!!
cout << "Test #" << ++k << ":\n";
if (n > m)
{
cout << 0 << endl;
continue;
}
if (n != 0)
{
BigMul(m - n + 1);
JC(m + n, m + 1);
}
else		//n为0的情况
{
JC(m, 0);
}
int j = 0;
while (!BigInt[j])j++;		//去掉前面的0
printf("%d", BigInt[j++]);		//最高位不需要补齐4位
while (j<=99)
{
printf("%04d", BigInt[j]);
j++;
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: