Harry Potter and the Hide Story
传送门(建议到 vjudge 上去提交)
iSea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough.
Input
The first line contains a single integer
T
T
T, indicating the number of test cases.
Each test case contains two integers,
N
N
N and
K
K
K.
Technical Specification
- 1 ≤ T ≤ 500 1 \leq T \leq 500 1≤T≤500;
- 1 ≤ K ≤ 100000000000000 1 \leq K \leq 1 000 000 000 000 00 1≤K≤100000000000000;
- 1 ≤ N ≤ 1000000000000000000 1 \leq N \leq 1 000 000 000 000 000 000 1≤N≤1000000000000000000。
Output
For each test case, output the case number first, then the answer, if the answer is bigger than
9223372036854775807
9 223 372 036 854 775 807
9223372036854775807, output inf
(without quote).
Sample
Input
2
2 2
10 10
Output
Case 1: 1
Case 2: 2
翻译
给定 N N N 和 K K K,求 n ! m o d k i n!\mod k^i n!modki 等于 0 0 0 时, i i i 的最大取值。
解题思路
前置知识
正文
知道了勒让德定理,那就好办了。直接把 K K K 进行质因数分解,让后套勒让德定理,求出该质因子在 N ! N! N! 中的指数。最后再将这些指数求最小值即可。(就这么短)
注意:HDU 不能用万能头啊!
(日常派大星:1/1)
AC Code
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
#define int long long
bool Mark[10000005];
int Prime_List[10000005];
int Prime_List_Length;
inline void Make_Prime_List() {
memset(Mark,0,sizeof Mark);
Prime_List_Length=0;
for(register int i=2; i<=10000005; ++i) {
if(!Mark[i])
Prime_List[++Prime_List_Length]=i;
for(register int j=1; j<=Prime_List_Length&&i*Prime_List[j]<10000005; ++j) {
Mark[i*Prime_List[j]]=1;
if(i%Prime_List[j]==0)
break;
}
}
}
int n,k;
map<int,int> Map;
int Answer;
inline map<int,int> Get_Prime_Sum(int n) {
map<int,int> res;
for(register int i=1; i<Prime_List_Length; ++i) {
if(Prime_List[i]>n)
break;
while(n%Prime_List[i]==0)
++res[Prime_List[i]],
n/=Prime_List[i];
}
if(n!=1)
res[n]=1;
return res;
}
inline void Work() {
cin>>n>>k;
if(k==1) {
cout<<"inf"<<endl;
return;
}
Map.clear(),
Answer=2e18;
Map=Get_Prime_Sum(k);
int t,num,tmp,n_tmp;
for(register map<int,int>::iterator it=Map.begin(); it!=Map.end(); ++it) {
t=it->first,num=it->second,tmp=0,n_tmp=n;
while(n_tmp>0)
tmp+=n_tmp/t,
n_tmp/=t;
tmp/=num;
Answer=min(Answer,tmp);
}
cout<<Answer<<endl;
}
signed main() {
Make_Prime_List();
int t;
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0),cin>>t;
int now=0;
while(t--) {
++now;
printf("Case %d: ",now);
Work();
}
return 0;
}
资料出处
- [ 1 ] [1] [1]:CSDN;
- [ 2 ] [2] [2]:OI-Wiki。