now i got RE..and most of the cases gives the right answers..And i thought my arithmetic seem no mistakes..I used it and got AC in 369 before..My arithmetic is to calculate the number of the emement of factorial..But it may crash in this case: 9111 1;It will give the wrong answers..i can't find what mistake i have made..i 've confused..help..thanks in advance~
#include <iostream.h>
#include "math.h"
typedef struct
{
int prime;
int num;
}Prime;
bool IfPrime (int n)
{
int limit;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
limit = sqrt (n) + 1;
for (int i = 3; i < limit; i = i + 2)
if (n % i == 0)
return false;
return true;
}
int main ()
{
int n, m;
int i, j, temp;
long long result;
Prime p[6544];
int pNum = 0;
for (i = 0; i <= 65536; i++)
if (IfPrime (i))
p[pNum++].prime = i;
while (cin>>n>>m && n||m)
{
result = 1;
for (i = 1; i < 6544; i++)
p[i].num = 0;
for (i = 2; i <= n; i++)
{
temp = i;
for (j = 1; temp != 1; j++)
{
while (temp % p[j].prime == 0)
{
p[j].num++;
temp = temp / p[j].prime;
}
}
}
for (i = 2; i <= m; i++)
{
temp = i;
for (j = 1; temp != 1; j++)
{
while (temp % p[j].prime == 0)
{
p[j].num--;
temp = temp / p[j].prime;
}
}
}
for (i = 2; i <= n - m; i++)
{
temp = i;
for (j = 1; temp != 1; j++)
{
while (temp % p[j].prime == 0)
{
p[j].num--;
temp = temp / p[j].prime;
}
}
}
for (i = 1; i < 26; i++)
{
for (j = 0; j < p[i].num; j++)
result *= p[i].prime;
}
cout<<result<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int gcd (int u, int v)
{
int t;
while (u>0)
{
if (u<v) {t=u;u=v;v=t;}
u=u-v;
}
return v;
}
int main ()
{
long long double n,m,r,h;
bool first=true;
vector <int> n1,m1;
while (cin>>n>>m)
{
if (n==0 && m==0) break;
if (!first) cout<<endl; else first=false;
r=1;
n1.clear();
m1.clear();
for (int i=n-m+1;i<n+1;i++)
{n1.push_back(i);}
for (int i=2;i<m+1;i++)
{m1.push_back(i);}
for (int i=0;i<n1.size();i++)
{
for (int j=0;j<m1.size();j++)
{
h=gcd(n1[i],m1[j]);
if (n1[i]>1 && m1[j]>1 && h>1)
{
n1[i]/=h;m1[j]/=h;
}
}
}
for (int i=0;i<n1.size();i++) {r*=n1[i];}
cout<<r<<endl;
}
return 0;
}
And can some one say me why do i get WA at 369 because of this. the code is the same except the
cout<<r<<endl; being cout<<n<<" things taken "<<m<<" at a time is "<<r<<" exactly.";
#include<iostream>
using namespace std;
int main()
{
int n,m,maior;
double numerador,denominador, res;
long long c;
cin >> n >> m;
while( n != 0 && m != 0)
{
numerador = denominador = res =1;
c = 0;
maior = max(m,n-m);
for(numerador = n, denominador = n - maior; numerador > maior; numerador--,denominador--)
{
res = res * (numerador/denominador);
}
cout << n <<" things taken "<< m << " at a time is " << (long long)res << " exactly."<< endl;
cin >> n >> m;
}
return 0;
}