牛客 阶乘 ###K ###K //K

题目链接:https://ac.nowcoder.com/acm/contest/5505/C

思路:首先考虑枚举 然后看到数据范围想二分

然后尝试去找是否有单调性 首先根据唯一分解定理 把要求的p质因数分解

然后统计出他的各个素数幂 和要check的mid比较,如果要满足倍数关系的话,

那么p的每一个素数的幂都小于等于mid的每一个素数的幂  并且随着mid的增大 各个素数的幂是只增不减的 所以也满足单调性

那么难点在于怎么求n的阶乘的素因子的幂了 即求1~n n个数中 的素因子的幂

假设n为8 要求的素数是2

那么让8/2  即让 前面是2的倍数的 2 4 6 8 变为了 1 2 3 4 然后继续除2 直到n为0

这样便可以让每一次都提取前面的满足的倍数 同时除以那个素数又可以让n本身缩减一次

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =2e5+10;
 6 const int mod=1e9+7;
 7 map<int,int>mp;
 8 int calc(int x,int s)
 9 {
10     int cnt=0;
11     while(x)
12     {
13         x/=s;
14         cnt+=x;
15     }
16     return cnt;
17 }
18 int check(int x)
19 {
20 
21     for(auto &v:mp)
22     {
23         if(calc(x,v.first)<v.second)
24             return false;
25     }
26     return true;
27 }
28 int main()
29 {
30     ios::sync_with_stdio(false);
31     cin.tie(0);
32     int t;
33     cin>>t;
34     while(t--)
35     {
36         mp.clear();
37         int p;
38         cin>>p;
39         for(ll i=2;i*i<=p;i++)
40         {
41             if(p%i==0)
42             {
43                 while(p%i==0)
44                 {
45                     mp[i]++;
46                     p/=i;
47                 }
48             }
49         }
50         if(p>1)
51             mp[p]++;
52         int l=1,r=1e9;
53         int ans=1;
54         while(l<=r)
55         {
56             int mid=(l+r)/2;
57             if(check(mid))
58             {
59                 ans=mid;
60                 r=mid-1;
61             }
62             else
63                 l=mid+1;
64         }
65         cout<<ans<<'\n';
66     }
67 
68 
69 
70 
71 }
View Code

 

posted @ 2020-06-02 13:31  canwinfor  阅读(204)  评论(0)    收藏  举报