HackerRank-Coprime Power Sum-类min25筛的DP

news/2024/10/9 2:53:33

link:https://www.hackerrank.com/challenges/coprime-power-sum/problem
题意:给一个长度为 \(n(1\leq n\leq 50)\) 的序列 \(s\),以及 \(0\leq k\leq 10\)\(1\leq m\leq 10^{12}\),对每个 \(i:1\leq i\leq m\),如果不是任何一个 \(s_j\) 的倍数,就加上 \(i^k\) 的贡献,求最后的贡献是多少,保证 \(s\) 两两互质


两两互质可以类似当素数来考虑:因为这样如果某个数 \(x\) 可以表示成 \(s\) 中若干数字的乘积,那么表示一定是唯一的。

类似min25筛的DP,设 \(G_i(n)\) 表示用前 \(i\)\(s\) 筛完 \(1,\dots,n\) 中的数字后的答案,显然 \(f(n)=n^k\) 是个完全积性函数,那么 \(G_i(n)=G_{i-1}(n)-\)这一次会被筛掉的部分的贡献,也就是 \(s_i^k\times G_{i-1}(\lfloor\frac{n}{s_i}\rfloor)\) ,这样就足够了,不重不漏,因为那些公倍数一定在之前就被筛过了。(这里和min25不一样,min25这部分的DP还带有一个“最小质因子”的限制)

然后就是自然数幂和的部分,Lagrange插值处理一下

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
constexpr int MOD=1e9+7;
template <unsigned M_>struct ModInt{static constexpr unsigned M=M_;unsigned x;constexpr ModInt():x(0U){}constexpr ModInt(int x_):x(((x_%=static_cast<int>(M))<0)?(x_+static_cast<int>(M)):x_){}constexpr ModInt(ll x_):x(((x_%=MOD)<0)?(x_+M):x_){}ModInt & operator +=(const ModInt &a){x=((x+=a.x)>=M)?(x-M):x;return *this;}ModInt & operator -=(const ModInt &a){x=((x-=a.x)>=M)?(x+M):x;return *this;}ModInt & operator *=(const ModInt &a){x=(static_cast<unsigned long long>(x)*a.x)%MOD;return *this;}ModInt & operator /=(const ModInt &a){return (*this*=a.inv());}ModInt pow(ll e)const{if(e<0)return inv().pow(-e);ModInt a=*this,b=1;for(;e;e>>=1){if(e&1)b*=a;a*=a;}return b;}ModInt inv()const{unsigned a=M,b=x;int y=0,z=1;for(;b;){const unsigned q=a/b;const unsigned c=a-q*b;a=b;b=c;const int w=y-static_cast<int>(q)*z;y=z;z=w;}assert(a==1U);return ModInt(y);}ModInt operator+()const {return *this;}ModInt operator-()const {ModInt a; a.x = x ? (M - x) : 0U; return a; }ModInt operator+(const ModInt &a)const { return (ModInt(*this) += a); }ModInt operator-(const ModInt &a)const { return (ModInt(*this) -= a); }ModInt operator*(const ModInt &a)const { return (ModInt(*this) *= a); }ModInt operator/(const ModInt &a)const { return (ModInt(*this) /= a); }template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x; }friend istream &operator>>(istream &is, ModInt &a) { return is >> a.x; }
};
typedef ModInt<MOD> Mint;
constexpr int N=105;
Mint fact[N],inv_fact[N];
vector<vector<Mint>> y;
Mint Lagrange(const long long & n,const int &k,const vector<Mint> &y){//static Mint pre[15],suf[15];// static vector<Mint> x(k+1);Mint ans=0;    // for(int i=0;i<=k;i++)x[i]=i;//pre[0]=n;suf[k+1]=1;for(int i=1;i<=k;i++)pre[i]=pre[i-1]*(n-i);for(int i=k;i>=0;i--)suf[i]=suf[i+1]*(n-i);for(int i=0;i<=k;i++){Mint up=(i==0?1:pre[i-1])*suf[i+1];Mint down=inv_fact[i]*inv_fact[k-i]*((k-i&1)?MOD-1:1);ans=(ans+y[i]*up*down) ;}return ans;
}
void init(){fact[0]=1;for(int i=1;i<N;i++)fact[i]=fact[i-1]*i;inv_fact[N-1]=fact[N-1].inv();for(int i=N-1;i>=1;i--)inv_fact[i-1]=inv_fact[i]*i;y.resize(11);for(int k=0;k<=10;k++){y[k].resize(k+10);y[k][0]=0;for(int i=1;i<=k+5;i++)y[k][i]=y[k][i-1]+Mint(i).pow(k);}
}
namespace Min25{constexpr int maxs=2e6;//2sqrt(n)long long global_n,lis[maxs + 1];int lim,cnt;int le[maxs+1],ge[maxs+1];Mint G[maxs+1];#define idx(v) (v<=lim?le[v]:ge[global_n/(v)])Mint init(const ll &n,const int &k,const vector<long long> &s){//calc Fprimeglobal_n=n;cnt=0;for(long long i=1,j,v;i<=n;i=n/j+1){j=n/i;v=j;lis[++cnt]=j;(j<=lim?le[j]:ge[n/j])=cnt;G[cnt]=Lagrange(j,k+1,y[k]);}for(int j=1;j<=s.size()-1;j++){auto p=s[j];auto pk=Mint(p).pow(k);for(int i=1;lis[i]>=p;i++){const int id=idx(lis[i]/p);G[i]-=pk*(G[id]);}}return G[idx(n)];}
}
void solve(){int n,k;long long m;cin>>n>>k>>m;vector<long long> s(n+1);rep(i,1,n)cin>>s[i];Min25::lim=sqrt(m);cout<<Min25::init(m,k,s)<<endl;
}
int main(){fastio;init();int tc;cin>>tc;while(tc--)solve();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/69228.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

31. 数据库基础

1. 数据库基础知识 1.1 关系型数据库与非关系型数据库1.2 关系型数据库的结构 库 Database 库,也称数据库,用于组织、存储和管理数据 类比于文件夹 表 Table 表,是数据库中基本的数据存储单位,由行(Row)和列(Column)组成 类比于excel文件 记录 Record 记录,是表中的一…

KeyShot基础操作2 - 材质篇

介绍了KeyShot的材质相关的内容:上材质、材质参数、贴图类型、映射类型、材质节点图等。​这部分基础操作,只是介绍KeyShot的操作方法,望知晓。 后续也会再更新材质、打光的案例,同时也会提供对应的工程文件。上材质基础操作 材质的通用参数 材质类型 纹理类型 多层材质 贴…

创建进程,设计信号量同步机制,实现多线程同步 - C语言版

环境:Windows11 编译器:Visual Studio 2019相关头文件: #include <windows.h> #include <stdio.h>相关函数:睡眠等待函数:Sleep(int millisecond); 睡眠等待一定时间,会造成OS重新调度其它的线程运行Sleep(10); //当前线程睡眠10毫秒后重新执行创建进程Cre…

Serilog文档翻译系列(七) - 应用设置、调试和诊断、开发接收器

Serilog支持通过App.config和Web.config中的01、应用设置 Serilog 支持在 App.config 和 Web.config 文件中使用简单的 配置语法,以设置最低日志级别、为事件添加额外属性以及控制日志输出。 Serilog 主要通过代码进行配置,设置支持旨在作为补充功能。虽然不是全面的,但大多…

古典+ezRSA

​ 古典密码在线工具:https://ctf.bugku.com/tools.html 一键解码工具库:随波逐流,在github上下载即可 注:古典密码只需做个了解,因为很多都是靠工具实现的,多刷题有个印象,遇到题能看出像什么密码就好。 Base家族 在密码学领域,"base" 通常指的是一种编码方…

【视频讲解】Python量子计算聚类Q-means:量子k-means算法分析电路数据实现可视化

全文链接:https://tecdat.cn/?p=37821 原文出处:拓端数据部落公众号 分析师:Yifan Zhang 量子计算在近期已然成为一个频繁出现的热门概念。尽管它在大众认知以及互联网社区中备受瞩目,热度极高,然而就其实际能力而言,当前仍然存在诸多局限。 量子计算作为一个全新的领域…

20222417 2024-2025-1 《网络与系统攻防技术》实验一实验报告

1.实验内容 (1).掌握反汇编与十六进制编程器 (2).能正确修改机器指令改变程序执行流程 (3).能正确构造payload进行bof攻击 2.实验过程 (1).直接修改程序机器指令,改变程序执行流程 将pwn1文件放入共享文件夹,后续在kali中使用,再将文件复制到实验文件夹share路径…

第一课 php基础语法 变量 函数

php语法<?php// 代码段   ?> php输出方法:echo 和 print不同点:echo-能够输出一个以上的字符串,英文逗号隔开print-只能输出一个字符串,并始终返回1echo 比 print 稍快,并且开销低 注释注释不会被作为程序来读取和执行。它唯一的作用是供代码编辑者阅读(让别人…