任意門:http://codeforces.com/contest/1114/problem/C
C. Trailing Loves (or L'oeufs?)
Aki is fond of numbers, especially those with trailing zeros. For example, the number 92009200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.
However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.
Given two integers nn and bb (in decimal notation), your task is to calculate the number of trailing zero digits in the bb-ary (in the base/radix of bb) representation of n!n! (factorial of nn).
The only line of the input contains two integers nn and bb (1≤n≤10181≤n≤1018, 2≤b≤10122≤b≤1012).
Print an only integer — the number of trailing zero digits in the bb-ary representation of n!n!
6 9
1
38 11
3
5 2
3
5 10
1
In the first example, 6!(10)=720(10)=880(9)6!(10)=720(10)=880(9).
In the third and fourth example, 5!(10)=120(10)=1111000(2)5!(10)=120(10)=1111000(2).
The representation of the number xx in the bb-ary base is d1,d2,…,dkd1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0x=d1bk−1+d2bk−2+…+dkb0, where didi are integers and 0≤di≤b−10≤di≤b−1. For example, the number 720720 from the first example is represented as 880(9)880(9) since 720=8⋅92+8⋅9+0⋅1720=8⋅92+8⋅9+0⋅1.
You can read more about bases here.
題意概括:
求 n! 轉化為 b 進制下末尾有多少個 0.
解題思路:
原題:swjtuOJ 2090
這是一道很好的數論題。
首先轉換一下思路:
要求 n! 在 b 進制下有多少個尾 0 就相當於 求 n! % (b^k) == 0 的最大 k。
那么我們現在把 n! 看作一個數 A。問題就是 求 A % (b^k) == 0 的最大 k;
我們知道有素數分解定理: b = p1^a1 * p2^a2 * p3^a3 ...;
那么我們如果可以求得 A 里面 p1^b1 * p2^b2 * p3^b3 ... 的 b1, b2, b3...
那么答案 ans = min(ans, ai/bi ) 了也就是要整除,首先要滿足最小的那個能整除。
(1)首先對 b 進行素因子分解,直接暴力(log b), 用一個數組離散化形成該素因子的編號和該素因子的冪的映射 或者 用map存儲該素因子的冪,得到所有素因子以及素因子的冪
(2)對於每一個素因子p,計算對應的 A(即 n! ) 中素因子p的冪,兩者相除取所有p冪的最小值就是對應的最大整數。
這里求 n! 下 素因子 p 的冪 用累除法,因為存在推論:
n! 下 p 的冪 = [ n/p ] + [ n/(p^2) ] + [ n/(p^3) ] ...
學習:
https://blog.csdn.net/Wen_Yongqi/article/details/86976902
AC code:
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<vector> 6 #include<queue> 7 #include<cmath> 8 #include<set> 9 #include<map> 10 #define INF 0x3f3f3f3f 11 #define LL long long 12 using namespace std; 13 const int MAXN = 2e6+1000; 14 LL N, B; 15 vector<LL>prime; 16 //LL num[MAXN]; 17 map<LL, int>mmp; 18 void get_p(LL n) 19 { 20 LL len = sqrt((double)n); 21 //printf("len %lld\n", len); 22 for(LL i = 2; i <= len; i++){ 23 if(n%i == 0){ 24 prime.push_back(i); 25 while(n%i == 0){ 26 n/=i; 27 //num[prime.size()-1]++; 28 mmp[i]++; 29 } 30 } 31 } 32 if(n != 1){ 33 prime.push_back(n); 34 // num[prime.size()-1]++; 35 mmp[n]++; 36 } 37 } 38 39 LL calc(LL n, LL p) 40 { 41 LL res = 0; 42 while(n){ 43 res += n/p; 44 n/=p; 45 //puts("zjy"); 46 } 47 return res; 48 } 49 50 int main() 51 { 52 LL sum = 1LL; 53 scanf("%I64d %I64d", &N, &B); 54 get_p(B); 55 LL ans = (1LL<<62); 56 for(LL i = 0; i < prime.size(); i++){ 57 // ans = min(ans, calc(N, prime[i])/num[i]); 58 //puts("zjy"); 59 ans = min(ans, calc(N, prime[i])/mmp[prime[i]]); 60 } 61 62 printf("%I64d\n", ans); 63 return 0; 64 }
