①普通的唯一分解定理
先预处理出prime[]里面装的是1~n的素数,然后分解x时,不断用x除以素数,直到x变成1。
这种方法时间比较长,可以进行剪枝,就是x在不断除以素数的过程中,当x变成了一个素数,就直接结束。
void Insert(int x) { for(int i=1;i<=num;i++) { if(x%prime[i]==0) { int cnt=0; while(x%prime[i]==0)x/=prime[i],cnt++; a[i]+=cnt; } if(x==1)break; if(f[x]==0)//f[x]=0说明x是素数 { a[P[x]]++;break;//P[x]是x所属的素数编号 } } }
②分解x!
当第i个素数为P 时,c[i]= n/p+n/(p*p)+n/(p*p*p)……(直到n/(p*……*p变成0)
void jie(int x) { for(ll i=1;i<=num;i++) { int P=prime[i]; while(P<=n) { c[i]+=x/P; P*=prime[i]; } } }