Educational Codeforces Round 138 (Rated for Div. 2) - D. Counting Arrays
数论 + 计数
题意
给定整数 \(n\;(1<=n<=3e5),\;m\;(1<=m<=1e12)\)
要求求长度为 \(n\) 的数组, 满足下列条件的个数
- \(1<=a[i]<=m\)
- 对于每个位置 \(i\), 当 \(\gcd(a[i],i)=1\) 时可删去这个元素,直到删完所有元素;这个删除的过程是唯一的(即每次删除都有且只有一个位置可以删)(某个元素被删除后,他后面的元素会补上来,下标都 - 1)
思路
- 因为 \(\gcd(a[1],1)=1\) 恒成立,所有每次删除都可以删第一个元素,要删除的过程是唯一的,则需要每次删除时只有第一个位置可以被删
- 对于第 \(i\) 个位置,因为每次都会删第一个元素,所以 \(y=a[i]\) 这个值会依次出现在 \(i,i-1,i-2...2,1\) 这些位置上,并且只有在 位置1 可以被删除,所以 \(a[i]\) 要与 \(1,2,3...,i\) 均不互质,即是 \([1,i]\) 中的所有质数的倍数
- 维护 \([1,i]\) 的质数积 \(tmp\) 即可,第 \(i\) 个位置可以取的数就是 \(\lfloor \frac m{tmp}\rfloor\); 注意 \(tmp>m\) 时就没必要再更新了,防止溢出
- 注意 \(m<=1e12\), 在计算答案时 \(m\) 需要先取模再计算!!!
代码
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
const int mod = 998244353;
ll n, m;
int pr[N / 5], p[N], cnt;
ll f[N], s[N];
void get_primes(int n)
{
p[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!p[i])
{
p[i] = i;
pr[++cnt] = i;
}
for (int j = 1; j <= cnt && pr[j] <= n / i; j++)
{
p[i * pr[j]] = pr[j];
if (p[i] == pr[j])
break;
}
}
}
ll qmi(ll a, ll b)
{
ll ans = 1;
while(b)
{
if (b & 1)
ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
get_primes(N - 10);
cin >> n >> m;
ll sub = 0;
ll tmp = 1;
s[0] = 1;
for (int i = 1; i <= n; i++)
{
if (p[i] == i && tmp <= m)
tmp *= i;
f[i] = m / tmp % mod;
s[i] = s[i-1] * f[i] % mod;
sub = (sub + s[i]) % mod;
}
ll ans = 0;
for (int i = 1; i <= n; i++)
ans = (ans + qmi(m % mod, i)) % mod;
ans = (ans - sub + mod) % mod;
cout << ans << endl;
return 0;
}
Educational Codeforces Round 138 (Rated for Div. 2) - D. Counting Arrays的更多相关文章
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi的的幂为kkk,则这个 ...
- Educational Codeforces Round 138 (Rated for Div. 2) A-E
比赛链接 A 题解 知识点:贪心. 注意到 \(m\geq n\) 时,不存在某一行或列空着,于是不能移动. 而 \(m<n\) 时,一定存在,可以移动. 时间复杂度 \(O(1)\) 空间复杂 ...
- Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays
求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_ ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
随机推荐
- 浅聊一下Django如何避免xss攻击
一.什么是xss攻击 xss攻击:----->web注入 xss跨站脚本攻击(Cross site script,简称xss)是一种"HTML注入",由于攻击的脚本多数时候是 ...
- 基于SqlSugar的开发框架循序渐进介绍(24)-- 使用Serialize.Linq对Lambda表达式进行序列化和反序列化
在上篇随笔<基于SqlSugar的开发框架循序渐进介绍(23)-- Winform端管理系统中平滑增加对Web API对接的需求>中介绍了基于一个接口,实现对两种不同接入方式(直接访问数据 ...
- Python实验报告(第9章)
实验9:异常处理及程序调试 一.实验目的和要求 1.了解代码异常知识: 2.掌握异常处理的try-except语句.try-except-else语句.try-except-finally语句.rai ...
- github的初体验
首先你得注册一个自己的GitHub账号,注册网址:https://github.com/join有了自己的账号以后,就可以进行登录,开始创建一个新的项目创建一个新的项目,填写项目名称,描述创建完成之后 ...
- S2-017 CVE-2013-2248
漏洞名称 Apache Struts 多个开放重定向漏洞 (CVE-2013-2248) s2-017 利用条件 Struts 2.0.0 - Struts 2.3.15 漏洞原理 通过操作前缀为&q ...
- Creator 2.x 升级 3.x 基础 API 差异总结
上一篇我们介绍了 Cocos Creator 2.x 项目升级 3.x 的大流程. 但最后一步,还需要手动将之前 2.x 写的函数注释一处处的放开. 并将 2.x 的代码写法改成 3.x 的,下面我们 ...
- ionic+vue+capacitor系列笔记--手机从安卓10升级到安卓11以后,之前的代码不管用了,无法跳转其他应用
之前手机是安卓10版本,没什么问题,升级以后,手机出现了一些异常,发现原来代码里写的跳转功能,无法使用了哦~~脑壳痛 解决方案 本项目:build.gradle targetSdkVersion 30 ...
- vue+div.canvas图像标注功能实现
main.js import Vue from 'vue' import 'vueui-widgets/dist/index.css' import VueUI from 'vueui-widgets ...
- Typora 最后一个免费版本
介绍 Typora 是一款轻量级的 Markdown 编辑器,其最为出众的特点是: 所见即所得. Typora 于2021年11月23日推出了第一个正式版,并转为收费.不过价格也算合理,89元/3台设 ...
- 软工综合实践课设——员工招聘系统(参考BOSS直聘);Pyhton实现
应用背景: 随着科学技术的发展,岗位数量越来越多,特别是每逢毕业季找工作的人数也很多,如果人们找工作或者企业招人靠纯手工的话,费时费力,仅仅是筛选简历和费劲,并且员工找工作投简历可能得需要克服时间和空 ...