Uva10780 (decomposition prime factor)

Source: Internet
Author: User

High precision can be directly used for brute force.

You can also choose not to use high precision:

Decompose m into a prime factor and record each factor and its number of times. Then, calculate the number of occurrences of each factor in the factorial of N, and divide the number by the number of occurrences in m, which is the possible K value. Obtain the minimum K.

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<set>#include<vector>#include<algorithm>#include<stack>#include<queue>using namespace std;#define INF 1000000000#define eps 1e-8#define pii pair<int,int>#define LL long long intint m,n,k,T;int prime[5005],vis[5005];void getprime(){    int num=0;    for(int i=2;i<5000;i++)    {        if(vis[i]==0) prime[num++]=i;        for(int j=i*i;j<5000;j+=i)        {            if(j%i==0) vis[j]=1;        }    }}int main(){    //freopen("in6.txt","r",stdin);    //freopen("out.txt","w",stdout);    getprime();    scanf("%d",&T);    for(int cas=1;cas<=T;cas++)    {        scanf("%d%d",&m,&n);        k=INF;        vector<pii>zhi;        for(int i=0;prime[i]<=m;i++)        {            int num=0;            while(m%prime[i]==0)            {                num++;                m/=prime[i];            }            if(num)            zhi.push_back(make_pair(prime[i],num));        }        for(unsigned int i=0;i<zhi.size();i++)        {            int t=zhi[i].first,num=0;            for(int j=n;j>=1;j--)            {                int jj=j;                while(jj%t==0)                {                    num++;                    jj/=t;                }            }            //cout<<‘k‘<<num<<endl;            k=min(k,num/(zhi[i].second));        }        printf("Case %d:\n",cas);        if(k)        printf("%d\n",k);        else            printf("Impossible to divide\n");    }    //fclose(stdin);    //fclose(stdout);    return 0;}

 

Uva10780 (decomposition prime factor)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.