數論 D - 寒假安排 --acdream ACdream原創羣賽(12)のBUAA選拔賽

題:http://acdreamoj.sinaapp.com/

思路:By kuangbin

把K進行質因子分解下,就知道P(n,m) 中含有多少個該質因子了。

然後取最小值

比如 k = p1^k1 * p2^k2 …..pi^ki

 

如何求出P(n,m) 有多少個pi ,假如有x個,那麼所有的 x/ki 求最小值就是答案了



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
#include<math.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define eps 1e-7
 
int cnt;
int a[30],b[30];
void calc(ll k)
{
    ll i,j;
    for(i=2;i*i<=k;i++)
    {
        if(k%i==0)
        {
            a[cnt]=i;
            b[cnt]=0;
            while(k%i==0)
            {
                k/=i;
                b[cnt]++;
            }
            cnt++;
        }
    }
    if(k!=1)
    {
        a[cnt]=k;
        b[cnt]=1;
        cnt++;
    }
}
int solve(ll n,int x)
{
    ll sum=1;
    int ans=0;
    while(sum<=n)
    {
        sum*=x;
        ans+=n/sum;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,m,k,i,j;
        cin>>n>>m>>k;
        cnt=0;
        calc(k);
        int mn=inf;
        m=n-m;
        for(i=0;i<cnt;i++)
        {
            int tn,tm;
            tn=solve(n,a[i]);
            tm=solve(m,a[i]);
            tn-=tm;
            tn/=b[i];
            mn=min(tn,mn);
        }
        cout<<mn<<endl;
    }
}


發佈了212 篇原創文章 · 獲贊 3 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章