首页 > 其他分享 >CF1174E Ehab and the Expected GCD Problem

CF1174E Ehab and the Expected GCD Problem

时间:2024-01-27 21:44:06浏览次数:18  
标签:gcd times cdots Expected Ehab gets Problem

Ehab and the Expected GCD Problem

Luogu CF1174E

题目描述

\(p\) 是一个排列,定义 \(f(p)\) : 设 \(g_i\) 为 \(p_1, p_2, \cdots, p_i\) 的最大公因数(即前缀最大公因数),则 \(f(p)\) 为 \(g_1,g_2,\cdots,g_n\) 中不同的数的个数。

设 \(f_{max}(n)\) 为 \(1,2,\cdots,n\) 的所有排列 \(p\) 中 \(f(p)\) 的最大值,你需要求有多少 \(1,2,\cdots,n\) 的排列 \(p\) 满足 \(f(p)=f_{max}(n)\),答案对 \(10^9+7\) 取模。

Solution

考虑第一个数为 \(x\),那么为了使得不同的数的个数最多,将 \(x\) 进行质因数分解,每次一定只能将指数 \(-1\),否则一定不优。为了使得减的次数最多,那么 \(x\) 一定是由 \(2^a\times 3^b(b\in\{0,1\})\) 构成,因为如果出现 \(5\) 或 \(3^2\) 都可以变成 \(2^2\)。

考虑 DP 这一个前缀 \(\gcd\)。设 \(f(i,j,k)\) 表示当前在位置 \(i\),前缀 \(\gcd\) 为 \(2^j\times 3^k\) 的方案数。记 \(g(x)=\lfloor\dfrac n x\rfloor\) 表示 \(n\) 以内 \(x\) 的倍数的个数。那么有转移:

  • \(\gcd\) 不变,那么 \(f(i,j,k)\gets f(i-1,j,k)\cdot(g(2^j\times 3^k)-(i-1))\)。
  • \(\gcd\) 中 \(2\) 的指数 \(-1\),那么 \(f(i,j,k)\gets f(i-1,j+1,k)\cdot(g(2^j\times 3^k)-g(2^{j+1}\times 3^k))\)。
  • \(\gcd\) 中 \(3\) 的指数 \(-1\),那么 \(f(i,j,k)\gets f(i-1,j,k+1)\cdot(g(2^j\times 3^k)-g(2^j\times 3^{k+1}))\)。

时间复杂度 \(\mathcal O(n\log n)\)。

Code
int N, lst = 0, cur = 1;
mint f[2][21][2];
signed main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> N;
    int lg = __lg(N);
    f[cur][lg][0] = 1;
    if ((1 << (lg - 1)) * 3 <= N)
        f[cur][lg-1][1] = 1;
    For(i, 2, N) {
        swap(cur, lst);
        For(j, 0, lg) f[cur][j][0] = f[cur][j][1] = 0;
        For(j, 0, lg) {
            f[cur][j][0] += f[lst][j][0] * (N / (1 << j) - i + 1);
            f[cur][j][0] += f[lst][j+1][0] * (N / (1 << j) - N / (1 << (j + 1)));
            f[cur][j][0] += f[lst][j][1] * (N / (1 << j) - N / ((1 << j) * 3));
            f[cur][j][1] += f[lst][j][1] * (N / ((1 << j) * 3) - i + 1);
            f[cur][j][1] += f[lst][j+1][1] * (N / ((1 << j) * 3) - N / ((1 << (j + 1)) * 3));
        }
    }
    cout << f[cur][0][0] << '\n';
}

标签:gcd,times,cdots,Expected,Ehab,gets,Problem
From: https://www.cnblogs.com/hanx16msgr/p/17991976

相关文章

  • curl支持ssl报错:(60) SSL certificate problem: unable to get local issuer certific
     curl去访问https的站点报错:curl-vhttps://www.baidu.com*SSLv3,TLShandshake,Clienthello(1):*SSLv3,TLShandshake,Serverhello(2):*SSLv3,TLShandshake,CERT(11):*SSLv3,TLSalert,Serverhello(2):*SSLcertificateproblem:unabletogetl......
  • Expected type 'PublicFormat', got 'str' instead
    在用包cryptography进行非对称加密时,生成公钥的函数异常.fromcryptography.hazmat.backendsimportdefault_backendfromcryptography.hazmat.primitivesimportserializationfromcryptography.hazmat.primitives.asymmetricimportrsa#生成RSA密钥对defgenerate_rsa_ke......
  • 神经网络优化篇:详解局部最优的问题(The problem of local optima)
    局部最优的问题在深度学习研究早期,人们总是担心优化算法会困在极差的局部最优,不过随着深度学习理论不断发展,对局部最优的理解也发生了改变。向展示一下现在怎么看待局部最优以及深度学习中的优化问题。这是曾经人们在想到局部最优时脑海里会出现的图,也许想优化一些参数,把它们称......
  • F - Usual Color Ball Problems
    F-UsualColorBallProblemsProblemStatementYouaregivenpositiveintegers$N$,$M$,$K$,andasequenceofpositiveintegersoflength$N$,$(C_1,C_2,\ldots,C_N)$.Foreach$r=0,1,2,\ldots,N-1$,printtheanswertothefollowingproblem.......
  • ValueError: Found array with dim 4. None expected <= 2.
     Traceback(mostrecentcalllast): File"train.py",line109,in<module>   out,eval_res=tasks.eval_forecasting(model,data,train_slice,valid_slice,test_slice,scaler,pred_lens,n_covariate_cols,args.max_train_length-1) Fil......
  • spring boot一个奇怪的错误(There was an unexpected error (type=Internal Server Err
    今天运行springboot的时候爆了这个错(Therewasanunexpectederror(type=InternalServerError,status=500).Exceptionparsingdocument:template=“index”,line6-column3)说什么无法解析文档,昨天还运行的好好的,看一下控制台说什么meta标签没关闭,我可是用idea自己创......
  • CCPC-final 2019 Problem B - Infimum of Paths
    链接参考题解题意:求0->1路径上的数组成真小数最小值若最小路径无环,则长度\(\le\)n-1方法一从\(0\)开始,维护当前走到的点,每次都走边权(当前总体)最小的且\(v\)能到\(1\)的边,跑\(2n\)条边,顺便沿路维护走过的边权连\(1\rarr1\)代价为\(0\)的环,这样最后一定是在环......
  • 洛谷题单指南-模拟和高精度-P1601 A+B Problem
    原题链接:https://www.luogu.com.cn/problem/P1601题意解读:本题是高精度加法的模版题。知识点解析:  高精度加法:  如果一个数大到远超过整形变量的范围时,就不能使用int、long、longlong等变量来存储整数,也不能直接通过变量加法来求和。  因此,需要回到加法计算的本质,从个......
  • AtCoder Beginner Contest 335 G Discrete Logarithm Problems
    洛谷传送门AtCoder传送门考虑若我们对于每个\(a_i\)求出来了使得\(g^{b_i}\equiva_i\pmodP\)的\(b_i\)(其中\(g\)为\(P\)的原根),那么\(a_i^k\equiva_j\pmodP\)等价于\(kb_i\equivb_j\pmod{P-1}\),有解的充要条件是\(\gcd(b_i,P-1)\midb_j\)。显然......
  • CF1006E Military Problem 题解
    CF1006EMilitaryProblem题解题意给定一颗有\(n\thinspace(2\leqn\leq2\times10^5)\)个节点的树,树根为\(1\)。对于每个节点\(i\thinspace(2\leqi\leqn)\)都有它的父节点\(p_i\),并且每个节点的子节点都是按从小到大的顺序排列的的。有\(q\thinspace(1......