棋盘问题,思路就不说啦,老生常谈了(其实是想早点睡觉,现在已经3点了2333....
暴露出来的问题,关于有乘有除的式子的溢出问题,可以适当将乘除交换位置,(注意一定要保证过程中不产生小数) (wa了n次才发现233....
如本题中的:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll fact(int n);
int main(){
int n,k;
while (cin>>n>>k){
ll sum=0;
if (k>n)
cout<<-1<<endl;
else{
sum=fact(n)/fact(n-k);
int sum2=fact(n)/fact(k)/fact(n-k);
cout<<sum*sum2<<endl;
}
}
}
ll fact(int n){
ll fact=1;
for (int i=2;i<=n;i++)
fact*=i;
return fact;
}
sum=fact(n)/fact(n-k);
sum2=fact(n)/fact(k)/fact(n-k);
sum与sum2其实就是两个组合数(排列数) ,通过固定的公式得出,且必为整数
第一次我其实使用的sum=fact(n)/fact(n-k)*fact(n)/fact(k)/fact(n-k);
这样必出问题,如算到*fact(n)可能就溢出了,然后我傻傻地就将*fact(n)移到了后面,然后就出现了0........其实分开就行了。