"Power of n! Factor P" Swjtuoj 2090

Source: Internet
Author: User

"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
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Power of n! Factor P" Swjtuoj 2090

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.