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 给出一个字符串,现在可以对这个字符串进 ...
随机推荐
- 使用命令行运行用例时提示python.exe: Error while finding module specification for 'testcase_1.Test'.....
文件路径 输入命令 D:\demo>python -m unittest unittest_1/testcase_1.Test结果提示 ModuleNotFoundError: No modul ...
- python 小球碰撞游戏
#小球和挡板要自己找照片,放在一个单独文件夹,音乐也是一样的import pygame pygame.init()#游戏资源加载 a = 700#x轴为700 b = 800#y抽为800 sceee ...
- c++中编码protobuf repeated string
参考:http://www.cppblog.com/API/archive/2014/12/09/209070.aspx proto文件 addressbook.proto syntax = &quo ...
- [编程基础] C++多线程入门8-从线程返回值
原始C++标准仅支持单线程编程.新的C++标准(称为C++11或C++0x)于2011年发布.在C++11中,引入了新的线程库.因此运行本文程序需要C++至少符合C++11标准. 8 从线程返回值 8 ...
- python网络爬虫数据解析之正则
本节内容,讲解爬取网络图片,利用正则匹配图片地址 请求网页之后,响应部分内容如下图: 1 时间:2023/1/7 10:42 2 功能描述 3 1.进行指定标签的定位 4 2.标签或者标签对应的属性中 ...
- word取消保护
没有 文档的保护密码 可尝试用此方式,亲测有效 Excel.PPT 应该也可以,没试过 1.新建空白文档 2.插入.对象 3.点击[对象]右边的箭头,选择被加密的文件. 建议两个选项都试一下,我的第二 ...
- Word 交叉引用 给参考文献、图片题注添加引用
参考文献引用 假如想在红色[1]处添加引用,实现点击[1]跳到参考文献[1]. 首先需要将参考文献进行编号. 在需要插入引用的地方,选择插入-->交叉引用 点击插入 最后可以把[1]调成上标 上 ...
- Ubuntu 22.04 安装搜狗输入法
下载搜狗输入法 下载地址https://shurufa.sogou.com/linux 也可以命令下载 wget https://ime.sogouimecdn.com/202212182151/3b ...
- 複合語句塊——關於while循環的
看這兩個小段代碼: /*code1.*/ index=0; while(index<10) sam = 10*index+2; printf("sam=%d\n",sam); ...
- Luogu P6394 樱花,还有你题解
原题链接:樱花,还有你 $\scr{\color{DarkOrchid}{Solution}}$ Subtask1 这是一个送分的:总和都不到$n$,无论怎么收集,花瓣数肯定不到$n$,输出impos ...