"Power of n! Factor P" Swjtuoj 2090
"Note: Jiaotong University to see this article to learn to write their own, do not play for the game!" ~
Main topic
Number theory is a good question: give two large integers n,s (n<=10^18,s<=10^12), try to find the largest integer k make n! % S^k ==0
Number theory a good topic, it is easy to think of ideas, but the data will be larger, it is possible to explode a long long, the author because of the power of n! factor p uses the multiplication, in 10^12 around a prime number exploded, Qaq
So I still use tired division to the safe!
Talk about the idea.
- (1) The first factor decomposition of S, the Complexity O (LOGN), with map storage, to get all the element factor and the power of the element factor
- (2) For each factor p, calculate the corresponding n! of the factor P (complexity O (LOGN)), the two divides the minimum value of all P powers is the corresponding maximum integer k, the total time complexity is O (Logs Logn)
The power of the n! factor p is to be used with a tired division (⊙o⊙)!
Reference Code
/*====================================*|* power of n! Factor P (tired division) *|\*====================================*//*author:hacker_vision*/#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <map>#include <climits>#define CLR (k,v) memset (k,v,sizeof (k) )#define LL Long Long#define EPS 1e-8using namespace STD;Const int_max =1e3+Ten; ll N,s,t; map<ll,int>mp map<ll,int>:: Iterator it;voidDivide (ll N) {//Factor decompositionMp.clear (); t =0; for(LL i =2; I * I <= N; + + i) {if(n%i==0) {mp[i]++; N/=i; while(n%i==0) {mp[i]++; N/=i; } } }if(n!=1) mp[n]++;} ll judge (ll P) {the power of the//n! factor to decompose the prime number p, the division of Fatiguell cnt=0; ll now = n; while(now) {cnt + = now/p; Now/=p; }returnCNT;} ll solve () {ll ans =9223372036854775807lL for(it= mp.begin (); It!=mp.end (); ++it) {ans = min (judge (It->first)/it->second,ans); }returnAns;}intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge intTCin>>T; while(t--) {scanf("%lld%lld", &n,&s); Divide (s);printf("%lld\n", solve ()); }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Power of n! Factor P" Swjtuoj 2090