Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1279
Solution idea : factorial upto 20! can be easily stored into unsigned long long . ans is n! divided by the product of the factorials of equal characters.
Solution :
#include<bits/stdc++.h>
#define llu unsigned long long
using namespace std;
llu int fact[21];
llu int num[27];
char s[21];
int main()
{
fact[0]=1;
for(llu int i=1; i<21; i++)
{
fact[i]=fact[i-1]*i;
}
llu int t,l,res,ans,ca=1,val;
scanf("%llu",&t);
while(t--)
{
getchar();
memset(num,0,sizeof(num));
scanf("%s",s);
l=strlen(s);
for(llu int i=0; i<l; i++)
{
val=s[i]-'A';
num[val]++;
}
res=1;
for(llu int i=0; i<27; i++)
{
res=res*fact[num[i]];
}
ans=fact[l]/res;
printf("Data set %llu: %llu\n",ca,ans);
ca++;
}
}
Solution idea : factorial upto 20! can be easily stored into unsigned long long . ans is n! divided by the product of the factorials of equal characters.
Solution :
#include<bits/stdc++.h>
#define llu unsigned long long
using namespace std;
llu int fact[21];
llu int num[27];
char s[21];
int main()
{
fact[0]=1;
for(llu int i=1; i<21; i++)
{
fact[i]=fact[i-1]*i;
}
llu int t,l,res,ans,ca=1,val;
scanf("%llu",&t);
while(t--)
{
getchar();
memset(num,0,sizeof(num));
scanf("%s",s);
l=strlen(s);
for(llu int i=0; i<l; i++)
{
val=s[i]-'A';
num[val]++;
}
res=1;
for(llu int i=0; i<27; i++)
{
res=res*fact[num[i]];
}
ans=fact[l]/res;
printf("Data set %llu: %llu\n",ca,ans);
ca++;
}
}
No comments:
Post a Comment