您的位置:首页 > 大数据 > 人工智能

UVa - 10780 - Again Prime? No Time.

2014-09-09 18:58 302 查看
分解质因数处理。

用数组a和b分别保存n!和m的质因子的次幂。

整数x可以整除y时, x的各个质因子的次幂对应小于y的质因子次幂。

所以if(i是n!的质因子) res=min(a[i] / b[i]);

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cmath>
#include <iomanip>
typedef long long LL;
typedef unsigned long long LLU;
using namespace std;

vector<int> prime;
int a[10000+10], b[10000+10];

void init()
{
    prime.clear();
    int a[10111]={0};
    for(int i=2; i<=10000; i++)
        for(int j=i*i; j<=10000; j+=i)
            a[j]=1;
    for(int i=2; i<=10000; i++)
        if(!a[i])
            prime.push_back(i);
}

void fun(int x, int *c)
{
    int i=0;
    while(x!=1)
    {
        while(x%prime[i]==0)
        {
            c[prime[i]]++;
            x/=prime[i];
        }
        i++;
    }
}
int main()
{
    init();
    int t, kase=0;
    cin>>t;
    while(t--)
    {
        int m, n;
        cin>>m>>n;
        cout<<"Case "<<++kase<<":"<<endl;

        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        for(int i=1; i<=n; i++)
            fun(i, a);
        fun(m, b);

        int res=1e9;
        for(int i=1; i<=m; i++)
            if(b[i])
                res=min(a[i]/b[i], res);
        if(res==1e9 || res==0)
            cout<<"Impossible to divide\n";
        else
            cout<<res<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: