#include<iostream>
using namespace std;
long long num(int n,int m){
__int64 w=1,e=1;
if (n > m){
int r = m - 1;
for (int t = 0; t <= r; t++)
w *= (n - t);
for (int t = 0; t < m; t++)
e *= m - t;
return w / e;
}
else if (n == m||m==0)
return 1;
else
return NULL;
}
int main(){
int t;
int n=1;
long long s;
cin >> t;
while (t--){
cin >> n;
s=0;
if (n == 1)
cout << 1 << endl;
if (n == 2)
cout << 3 << endl;
if (n == 3)
cout << 8 << endl;
if (n >= 4 && n <= 20){
for (int i = 1; i <= n - 1; i++)
s += (n - i)*num(n-1, i);
cout << s+n<< endl;
}
}
}
第一眼看到这道题,以为很简单,直接暴力解题,发现数字经常溢出。再后来找规律,有些运算有时也会溢出,最后索性中间的运算步奏也用int64 。本题写了一个计算组合数的函数,在20以内结果都不会溢出.
人比较笨,只能用笨方法