溢位或者除以0的錯誤!!
Floating point exception (core dumped)
#include<iostream>
using namespace std;
unsigned long long int gcd(unsigned long long int a,unsigned long long int b){
if(b==0) return a;
else gcd(b,a%b);
}
int main(){
unsigned long long int N , M ;
while(cin>>N>>M){
int rM=M;
if(N==M and N==0) break;
if(M>=N/2){
M = N-M;
}
unsigned long long int a=1 ,i,tmp,g,g2;
for(i=1;i<=M;i++){
tmp = i;
g = gcd(a,tmp);
g2 = gcd((N-i+1),tmp);
if(g!=1 and g2!=1){
a = (a/g) * ((N-i+1)/g2) / (i/g/g2);
}
else if(g!=1){
a = (a/g) * (N-i+1) / (i/g);
}
else if(g2!=1){
a = a * ((N-i+1)/g2) / (i/g2);
}
else a = a * (N-i+1) / i;
// cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl;
}
cout << N << " things taken " << rM << " at a time is " << a << " exactly."<< endl;
}
}
#0: 100% RE (SIGFPE)
溢位或者除以0的錯誤!! Floating point exception (core dumped)
#include using namespace std; unsigned long long int gcd(unsigned long long int a,unsigned long long int b){ if(b==0) return a; else gcd(b,a%b); } int main(){ unsigned long long int N , M ; while(cin>>N>>M){ int rM=M; if(N==M and N==0) break; if(M>=N/2){ M = N-M; } unsigned long long int a=1 ,i,tmp,g,g2; for(i=1;i<=M;i++){ tmp = i; g = gcd(a,tmp); g2 = gcd((N-i+1),tmp); if(g!=1 and g2!=1){ a = (a/g) * ((N-i+1)/g2) / (i/g/g2); } else if(g!=1){ a = (a/g) * (N-i+1) / (i/g); } else if(g2!=1){ a = a * ((N-i+1)/g2) / (i/g2); } else a = a * (N-i+1) / i; // cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl; } cout << N << " things taken " << rM << " at a time is " << a << " exactly."<< endl; } }
更新 通過了
//UVA 00369 Combinations
#include<iostream>
using namespace std;
unsigned long long int gcd(unsigned long long int a,unsigned long long int b){
if(b==0) return a;
else gcd(b,a%b);
}
int main(){
unsigned long long int N , M ;
while(cin>>N>>M){
int rM=M;
if(N==M and N==0) break;
if(M>=N/2){
M = N-M;
}
unsigned long long int a=1 ,i,tmp,g,g2;
for(i=1;i<=M;i++){
tmp = i;
g = gcd(a,tmp);
g2 = gcd((N-i+1),tmp);
// cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl;
if(g!=1 and g2!=1 and i>=g*g2){
a = (a/g) * ((N-i+1)/g2) / (i/g/g2);
}
else if(g!=1){
a = (a/g) * (N-i+1) / (i/g);
}
else if(g2!=1){
a = a * ((N-i+1)/g2) / (i/g2);
}
else a = a * (N-i+1) / i;
// cout << "a= " << a << " (N-i+1)= " << N-i+1 << " i= " << i << " g= " << g << " g2= " << g2 <<endl;
}
cout << N << " things taken " << rM << " at a time is " << a << " exactly."<< endl;
}
}