题目链接:下沙的沙子有几粒?
解题思路:这题和1133的差不多,只不过每一个H和D都没有分别,所以上一题的基础之上,除去n!和m!。
#include<stdio.h> #include<string.h> #define MAX 1000 typedef struct A{ int num[MAX]; int len; A(){ memset(num, 0, sizeof(num)); len = 0; } A(int a){ memset(num, 0, sizeof(num)); len = 0; while(a){ num[len++] = a % 10; a /= 10; } } A(char kk[]){ int i, j, k; memset(num, 0, sizeof(num)); len = 0; for(i = strlen(kk) - 1; i >= 0; i--){ num[len++] = kk[i] - '0'; } } }Big_int; Big_int add(Big_int a, Big_int b){ Big_int ret = Big_int(); int rem = 0; int i, j, k; for(i = 0; i < MAX; i++){ if(!rem && !a.num[i] && !b.num[i]){ ret.len = i; break; } ret.num[i] = (rem + a.num[i] + b.num[i]) % 10; rem = (rem + a.num[i] + b.num[i]) / 10; } return ret; } Big_int subtract(Big_int a, Big_int b){ Big_int ret = Big_int(); int i, j, k; for(i = 0; i < MAX; i++){ if(!a.num[i] && !b.num[i]){ ret.len = i; break; } if(a.num[i] < b.num[i]){ int tem = i + 1; while(!a.num[tem]){ a.num[tem] = 9; tem++; } a.num[tem]--; ret.num[i] = 10 + a.num[i] - b.num[i]; } else{ ret.num[i] = a.num[i] - b.num[i]; } } return ret; } Big_int multiply(Big_int a, Big_int b){ Big_int ret = Big_int(); int i, j, k; for(i = 0; i < a.len; i++){ for(j = 0; j < b.len; j++){ ret.num[i + j] += a.num[i] * b.num[j]; } } int rem = 0; for(i = 0; i < MAX; i++){ int tem = rem + ret.num[i]; ret.num[i] = tem % 10; rem = tem / 10; } for(i = MAX - 1; ; i--){ if(ret.num[i] != 0){ ret.len = i + 1; break; } } return ret; } Big_int divide(Big_int a, int b){ Big_int ret; int i, j, k; int rem = 0; for(i = a.len - 1; i >= 0; i--){ ret.num[i] = (rem * 10 + a.num[i]) / b; rem = (rem * 10 + a.num[i]) % b; } for(i = MAX - 1; ; i--){ if(ret.num[i] != 0){ ret.len = i + 1; break; } } return ret; } int mod(Big_int a, int b){ int i, j, k, ret = 0; for(i = a.len - 1; i >= 0; i--){ ret = (a.num[i] % b + ((ret % b) * (10 % b)) % b) % b; } return ret; } Big_int power(Big_int a, int b){ Big_int ret = Big_int(1); int i, j, k; Big_int tem = Big_int(1); tem = multiply(tem, a); while(b){ if(b & 1){ ret = multiply(ret, tem); } b >>= 1; tem = multiply(tem, tem); } for(i = MAX - 1; ; i--){ if(ret.num[i] != 0){ ret.len = i + 1; break; } } return ret; } void display(Big_int a){ int i, j, k; if(a.len == 0){ printf("0"); } else{ for(i = a.len - 1; i >= 0; i--){ printf("%d", a.num[i]); } } printf("\n"); } int main(){ int i, j, k; int m, n, tot = 1; while(scanf("%d%d", &m, &n) != EOF){ Big_int ans = Big_int(1); Big_int tem1, tem2; for(i = 1; i <= m + n; i++){ ans = multiply(ans, Big_int(i)); } ans = multiply(ans, Big_int(m + 1 - n)); //display(ans); for(i = 2; i <= m + 1; i++){ if(i > n){ ans = divide(ans, i); continue; } ans = divide(ans, i * i); } display(ans); } return 0; }