CF#538 C - Trailing Loves (or L'oeufs?) /// 分解质因数

题目大意:

求n!在b进制下末尾有多少个0

https://blog.csdn.net/qq_40679299/article/details/81167283

一个数在十进制下末尾0的个数取决于10的幂的个数 即 1500=15*10^2 与两个0

在任意进制下也是 即n!在b进制下 n!=a*b^x 那么末尾0的个数就是 x

若b能分解出质因数 b1 b2 b3 ...

那么 a*b^x = a*(b1^x1 * b2^x2 * b3^x3 ... )^x = a*(b1^(x1*x) * b2^(x2*x) * b3^(x3*x) ... )

(x1表示能在b中分解出x1个b1...)

又可化成 A1*(b1^(x1*x)) 或A2*(b2^(x2*x))或 A3*(b3^(x3*x))的形式 即 A*B^X

对于特定的B 通过getcnt()可以求X 对于b1可求得X1 那么x=X1/x1

A的可能值有很多如A1 A2 A2 使得 n!=A*B^X解得的X也有多种

要使它们最终都满足 那么x应该是 X1/x1 X2/x2 X3/x3 ... 中最小的一个

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
const int mod=1e9+7;
const LL maxn=1e18+5;
const LL maxm=1e12+5;
LL n, b;
LL pri[10005], ind=0;
LL cnt[10005];
void get_pri(LL n) {
    mem(pri,0); mem(cnt,0);
    for(LL i=2;i*i<=n;i++) {
        while(n%i==0)
            pri[ind]=i, cnt[ind]++, n=n/i;
        if(cnt[ind]) ind++;
    }
    if(n>1) pri[ind]=n, cnt[ind++]=1;
} // 分解质因数
LL getcnt(LL p,LL n){
    LL res=0;
    while(n) res+=n/p, n/=p;
    return res;
} // n!能分解出res个p
int main()
{
    while(~scanf("%I64d%I64d",&n,&b)) {
        ind=0; get_pri(b); //printf("%d\n",ind);
        LL ans=maxn;
        for(LL i=0;i<ind;i++)
            ans=min(ans,getcnt(pri[i],n)/cnt[i]);
        printf("%I64d\n",ans);
    }

    return 0;
}

CF#538 C - Trailing Loves (or L'oeufs?) /// 分解质因数

原文地址:https://www.cnblogs.com/zquzjx/p/10360784.html

时间: 2024-08-02 22:44:31

CF#538 C - Trailing Loves (or L'oeufs?) /// 分解质因数的相关文章

C. Trailing Loves (or L&#39;oeufs?) (质因数分解)

C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行质因数分解后,每个因数个数的最小值 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; #define N 1000005 ll pri[N]; ll cnt[N]; ll tot; void getpri(

cf1114 C. Trailing Loves (or L&#39;oeufs?)

注意题目中一旦有大于1e9的数字,所有变量全用Longlong替代,包括i,j 或者一开始直接define int longlong ,然后之后主函数用int32_t,输入输出用int32_t替代 #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll INF=1e18+7; ll p[1000010]; ll c[1000010]; ll res=INF; int main() { ll n,x;

【Codeforces 1114C】Trailing Loves (or L&#39;oeufs?)

[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/81167283 这题的话m比较大, 做个质因数分解就ok>_< 算n!有多少个x因子的话 以5为例子 (n=25) 25 20 15 10 5 把他们都除5 5 4 3 2 1 然后再除5 1 所以总共有6个 转换成代码就是 while(n>0){ ans+=n/5; n = n/5; } [代码

Trailing Loves (or L&#39;oeufs?) CodeForces - 1114C (数论)

大意: 求n!在b进制下末尾0的个数 等价于求n!中有多少因子b, 素数分解一下, 再对求出所有素数的最小因子数就好了 ll n, b; vector<pli> A, res; void factor(ll x) { int mx = sqrt(x+0.5); REP(i,2,mx) if (x%i==0) { int t = 0; while (x%i==0) x/=i,++t; A.pb(pli(i,t)); } if (x>1) A.pb(pli(x,1)); } int main

CF-1114C-Trailing Loves (or L&#39;oeufs?)

题意: 输入n和m,求n!转换成m进制之后末尾有多少个0: 思路: 转换一下题意就可以看成,将n表示成x * (m ^ y),求y的最大值.^表示次方而不是异或: 这就比较好想了,将m分解质因数,对于每个质因数,设n!含有a个,m含有b个,则ans = min(ans, a / b); 自己比赛的时候写的 C - Trailing Loves (or L'oeufs?) GNU C++11 Accepted 46 ms 0 KB #include "bits/stdc++.h" usi

Codeforces 1114 - A/B/C/D/E/F - (Undone)

链接:http://codeforces.com/contest/1114 A - Got Any Grapes? 题意:甲乙丙三个人吃葡萄,总共有三种葡萄:绿葡萄.紫葡萄和黑葡萄,甲乙丙三个人至少要各自吃 $x,y,z$ 个葡萄,又甲只吃绿葡萄,乙不吃黑葡萄,丙三种颜色都吃.现在总共有 $a$ 个绿葡萄.$b$ 个紫葡萄.$c$ 个黑葡萄.要确认这么多葡萄能否使得三个人都满意. 题解:优先满足甲,剩下的绿葡萄加紫葡萄满足乙,最后再剩下的看看能不能满足丙. AC代码: #include<bits

Codeforces Round #538 (Div. 2) (CF1114)

Codeforces Round #538 (Div. 2) (CF1114) ??今天昨天晚上的cf打的非常惨(仅代表淮中最低水平 ??先是一路缓慢地才A掉B,C,然后就开始杠D.于是写出了一个O(n^2)的线性dp,然后就wa6,调到结束.结束后发现完全看漏了两句话.噢,起始点!!! ??好吧然后算算自己有可能这一场要变成+0,反正在0左右. 结束后开始然后开始写D,顺便思考F.结果写完D发现A怎么fst了,然后...因为习惯于对相似的语句复制粘贴,有些东西没有改--三句话都在 -a!!!(

CodeForces Contest #1114: Round #538 (Div. 2)

比赛传送门:CF #1114. 比赛记录:点我. 又 FST 了. [A]Got Any Grapes? 题意简述: 有三个人,第一个人需要吃绿色葡萄至少 \(a\) 个,第二个人需要吃绿色和紫色葡萄至少 \(b\) 个,第三个人需要吃绿色.紫色和黑色葡萄至少 \(c\) 个. 有 \(x\) 个绿色葡萄,\(y\) 个紫色葡萄,\(z\) 个黑色葡萄,问是否能够满足三个人的要求. 题解: #include <cstdio> int main() { int x, y, z, a, b, c;

Codeforces Round #538 (Div. 2)

目录 Codeforces 1114 A.Got Any Grapes? B.Yet Another Array Partitioning Task C.Trailing Loves (or L'oeufs?) D.Flood Fill(区间DP) E.Arithmetic Progression(交互 二分 随机化) F.Please, another Queries on Array?(线段树 欧拉函数) Codeforces 1114 比赛链接 貌似最近平均难度最低的一场div2了...