CF-1114C-Trailing Loves (or L'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"
    using namespace std;
    typedef long long LL;
    map<LL, LL> mp, num;
    vector<LL> vc;
    int main() {
        LL n, m;
        scanf("%lld%lld", &n, &m);
        for (LL i = 2; i * i <= m; i++) {
            if (m % i == 0) {
                vc.push_back(i);
                while (m % i == 0) {
                    m /= i;
                    mp[i]++;
                }
            }
        }
        if (m != 1) {
            vc.push_back(m);
            mp[m]++;
        }
        for (int i = 0; i < vc.size(); i++) {
            LL j = 1;
            while (j <= n / vc[i]) {
                j *= vc[i];
                num[vc[i]] += n / j;
            }
        }
        LL ans = 1LL << 62;
        for (LL i = 0; i < vc.size(); i++) {
            ans = min(ans, num[vc[i]] / mp[vc[i]]);
        }
        printf("%lld\n", ans);
        return 0;
    }

    其实没必要把各个因子保存下来。标程还是优很多的

  • 看了标程之后改的
    C - Trailing Loves (or L‘oeufs?) GNU C++11 Accepted 31 ms 0 KB
    #include "bits/stdc++.h"
    using namespace std;
    typedef long long LL;
    const LL INF = 1LL << 60;
    int main() {
        LL n, m, ans = INF;
        scanf("%lld%lld", &n, &m);
        for (LL i = 2; i <= m; i++) {
            if (i * i > m) {
                i = m;
            }
            if (m % i == 0) {
                int cnt = 0;
                while (m % i == 0) {
                    m /= i;
                    cnt++;
                }
                LL tmp = 0, mul = 1;
                /*
                 for (LL mul = i; mul <= n; mul *= i)
                 这种写法应该更符合正常思维,但是因为n最高可以达到1e18,比较接近LL上限,mul可能乘i之前还小于n,乘完就爆LL了;
                */
                while (mul <= n / i) {
                    mul *= i;
                    tmp += n / mul;
                }
                ans = min(ans, tmp / cnt);
            }
        }
        printf("%lld\n", ans);
        return 0;
    }

CF-1114C-Trailing Loves (or L'oeufs?)

原文地址:https://www.cnblogs.com/Angel-Demon/p/10364600.html

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

CF-1114C-Trailing Loves (or L'oeufs?)的相关文章

CF#538 C - Trailing Loves (or L&#39;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) *

【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

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;

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(

CF A. DZY Loves Hash

A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n numbers, in the order they are given, i

Cf 444C DZY Loves Colors(线段树)

DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of thei-th unit of the ribbon is i at first. It is colorful enough, but w

Cf 444C DZY Loves Colors(段树)

DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of thei-th unit of the ribbon is i at first. It is colorful enough, but w

(CF)Codeforces445A DZY Loves Chessboard(纯实现题)

转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://codeforces.com/problemset/problem/445/A DZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, oth