过了蛮多测试点的,后面测试数据比较大的时候就MLE了
这个是1.0代码
#include<iostream>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1000010;
int prime[N],n,cnt,minp[N],x;
long long ans;
int fact[100],sum[100001];
bool st[N];
int jc(int n){
return n==1?1:jc(n-1)*n;
}
void getprime(int n){
for(int i=2;i<=n;i++){
if(!st[i])prime[cnt++]=i,minp[i]=i;
for(int j=0;prime[j]*i<=n;j++){
st[prime[j]*i]=true;
minp[prime[j]*i]=prime[j];
if(i%prime[j]==0)break;
}
}
}
int main(){
getprime(N-1);
while(scanf("%d",&x)!=-1){
memset(fact,0,sizeof fact);
memset(sum,0,sizeof sum);
int k=0,tot=0;
while(x>1){
int p=prime[k];
//cout<<"k="<<k<<endl;
//cout<<"p="<<p<<endl;
while(x%p==0){
x/=p;
tot++;
sum[k]++;
//cout<<"x="<<x<<" sum[k]="<<sum[k]<<endl;
}
k++;
}
cout<<tot;
ans=jc(tot);
for(int i=0;tot>0;tot-=sum[i]){
ans/=jc(sum[i]);
}
printf(" %lld\n",ans);
}
}
这个是2.0代码
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = (1 << 20) + 10;
int primes[N], cnt;
int minp[N];
bool st[N];
void get_primes(int n)
{
for (int i = 2; i <= n; i ++ )
{
if (!st[i])
{
minp[i] = i;
primes[cnt ++ ] = i;
}
for (int j = 0; primes[j] * i <= n; j ++ )
{
int t = primes[j] * i;
st[t] = true;
minp[t] = primes[j];
if (i % primes[j] == 0) break;
}
}
}
int main()
{
get_primes(N - 1);
int fact[30], sum[N];
int x;
while (scanf("%d", &x) != -1)
{
int k = 0, tot = 0;
while (x > 1)
{
int p = minp[x];
fact[k] = p, sum[k] = 0;
while (x % p == 0)
{
x /= p;
sum[k] ++ ;
tot ++ ;
}
k ++ ;
}
LL res = 1;
for (int i = 1; i <= tot; i ++ ) res *= i;
for (int i = 0; i < k; i ++ )
for (int j = 1; j <= sum[i]; j ++ )
res /= j;
printf("%d %lld\n", tot, res);
}
return 0;
}
作者:yxc
链接:https://www.acwing.com/activity/content/code/content/197104/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。