Harry Potter and the hide story
Problem descriptionisea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough.
Inputthe first line contains a single integer T, indicating the number of test cases.
Each test case contains two integers, N and K.
Technical Specification
1. 1 <= T <= 500
2. 1 <= k <= 1 000 000 000 00
3. 1 <= n <= 1 000 000 000 000 000
Outputfor each test case, output the case number first, then the answer, if the answer is bigger than 9 223 372 036 854 775 807, output "INF" (without quote ).
Sample Input
22 210 10
Sample output
Case 1: 1Case 2: 2
Author [email protected]
Source2011 multi-university training contest 15-host by whu
Recommendlcy | we have carefully selected several similar problems for you: 3987 3986 3983 3984
Question:
Given N and K, evaluate n! When % K ^ I is equal to 0, what is the maximum value of I?
Solution:
Break down the key factor, and then obtain the link restriction I based on the key factor, and finally calculate the maximum I.
Solution code:
#include <iostream>#include <cstdio>#include <map>#include <vector>#include <cstring>using namespace std;typedef unsigned long long ll;ll n,k;const int maxn=10000010;bool isPrime[maxn];vector <ll> v;ll tol;void get_prime(){ tol=0; memset(isPrime,true,sizeof(isPrime)); for(ll i=2;i<maxn;i++){ if(isPrime[i]){ tol++; v.push_back(i); } for(ll j=0;j<tol && i*v[j]<maxn;j++){ isPrime[i*v[j]]=false; if(i%v[j]==0) break; } } //for(ll i=v.size()-1;i>=v.size()-100;i--) cout<<v[i]<<endl;}map <ll,ll> getPrime(ll x){ map <ll,ll> mp; for(ll i=0;i<tol && x>=v[i];i++){ while(x>0 && x%v[i]==0){ x/=v[i]; mp[v[i]]++; } } if(x>1) mp[x]++; return mp;}void solve(){ if(k==1){ printf("inf\n"); return; } map <ll,ll> mp=getPrime(k); ll ans=1e19; for(map <ll,ll>::iterator it=mp.begin();it!=mp.end();it++){ ll tmp=n,sum=0; while(tmp>0){ sum+=tmp/(it->first); tmp/=(it->first); } if(sum/(it->second)<ans) ans=sum/(it->second); } cout<<ans<<endl;}int main(){ get_prime(); int t; scanf("%d",&t); for(int i=0;i<t;i++){ cin>>n>>k; printf("Case %d: ",i+1); solve(); } return 0;}