【洛谷】P5348 密码解锁
【洛谷】P5348 密码解锁
很显然我们可以推导出这个式子
设\(a(m)\)为\(m\)位置的值
a(m) = \sum_{m|d}\mu(\frac{d}{m})\mu(d) \\
a(m) = \sum_{i = 1}^{\lfloor \frac{n}{m} \rfloor} \mu(i)\mu(im) \\
a(m) = \mu(m) \sum_{i = 1}^{\lfloor \frac{n}{m} \rfloor} \mu(i)^{2}[gcd(m,i) == 1]
\]
而\(\mu(i)^{2}\)的本质是无平方因子数,这个可以容斥
容斥的方法是(若没有其他限制)
\]
那么这里的就是
\]
前面的互质可以直接枚举
后面的互质可以通过莫比乌斯反演外加预处理M中莫比乌斯值不为0的数算出来
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 N,MAXV;
int M,mu[1000005];
int prime[1000005],tot;
bool nonprime[1000005];
vector<pii > division;
int gcd(int a,int b) {
return b == 0 ? a : gcd(b,a % b);
}
int Mu(int x) {
if(x <= 1000000) return mu[x];
int res = 1;
for(int i = 2 ; i <= x / i ; ++i) {
if(x % i == 0) {
int c = 0;
while(x % i == 0) {x /= i;++c;}
if(c >= 2) return 0;
res = -res;
}
}
if(x != 1) res = -res;
return res;
}
int64 calc(int64 n) {
int64 res = 0;
for(auto t : division) {
if(n < t.fi) break;
res += 1LL * t.se * (n / t.fi);
}
return res;
}
void Init() {
mu[1] = 1;
for(int i = 2 ; i <= 1000000 ; ++i) {
if(!nonprime[i]) {
prime[++tot] = i;
mu[i] = -1;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > 1000000 / i) break;
nonprime[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
else mu[i * prime[j]] = -mu[i];
}
}
}
void Solve() {
read(N);read(M);
if(Mu(M) == 0) {puts("0");return;}
division.clear();
for(int i = 1 ; i <= M / i ; ++i) {
if(M % i == 0) {
int j = M / i;
int x = Mu(i),y = Mu(j);
if(x) division.pb(mp(i,x));
if(i != j && y) division.pb(mp(j,y));
}
}
sort(division.begin(),division.end());
int64 T = N / M,res = 0;
for(int i = 1 ; i <= T / i ; ++i) {
if(gcd(i,M) == 1) {
res += Mu(i) * calc(T / (i * i));
}
}
res = res * Mu(M);
out(res);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
int T;
read(T);
for(int i = 1 ; i <= T ; ++i) Solve();
}
【洛谷】P5348 密码解锁的更多相关文章
- 洛谷T8116 密码
T8116 密码 题目描述 YJC把核弹发射密码忘掉了……其实是密码被加密了,但是YJC不会解密.密码由n个数字组成,第i个数字被加密成了如下形式:第k小的满足(2^L)|(P-1)且P为质数的P.Y ...
- 洛谷T90444 密码 题解
[问题描述] 假发通过了不懈的努力,得到了将军家门锁的密码(一串小写英文字母).但是假发被十四和猩猩他们盯上了,所以假发需要把密码传递出去.因为假发不想十四他们发现几松门前贴的小纸条就是将军家的密码, ...
- [P5348]密码解锁
Description 给一个长度为 \(n\) 的数组 \(a[1\dots n]\) ,满足 \(\sum_{m|x}a[x] = \mu(m)\),求 \(a[m]\). \(n\le 10^{ ...
- 洛谷 U6850 手机密码
U6850 手机密码 题目背景 小明的手机上设了一个由四个数字组成的密码,但是小明自己的记性不好,但又不想把密码直接记在纸上,于是便想了一个方法. 题目描述 小明有四行数字,每行数字都有n[i](&l ...
- 洛谷 P1703 那个什么密码2
P1703 那个什么密码2 题目背景 https://www.luogu.org/problem/show?pid=1079 题目描述 与原题一模一样.具体不同请见输入格式 输入输出格式 输入格式: ...
- 【Luogu5348】密码解锁(莫比乌斯反演,数论)
[Luogu5348]密码解锁(莫比乌斯反演,数论) 题面 洛谷 题解 首先题目给定的限制是\(\sum_{n|i}a[i]=\mu(n)\),然后把这个东西反演一下, 莫比乌斯反演的式子是:\(g( ...
- [洛谷日报#204] StackEdit——Markdown 编辑器的功能介绍
本文同时发表于洛谷日报,您也可以通过洛谷博客进行查看. 1.介绍与开始使用 1.1 这是什么? StackEdit是基于PageDown.Stack Overflow和其他堆栈交换站点使用的Markd ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
随机推荐
- c++ demo code
/* //多继承 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void s ...
- cmake语法入门记录
刚刚开始学习ROS,打算入机器人的坑了,参考教材是<ROS及其人开发实践>胡春旭编著 机械工业出版社 华章科技出品.本来以为可以按照书上的步骤一步步来,但是,too young to si ...
- Flask-login 原理
1 login_required 内部原理,主要是判断当前用户是否已经授权访问,如果没被授权就调用current_app.login_manager.unauthorized() current_us ...
- 修改oracle用户登录密码
运行sqlplus进入输入密码界面 用户名输入: connect as sysdba 密码:这边乱输就可以了 然后进行输入下面的命令: 修改密码命令 alter user system identif ...
- 拆分项目搞成framework 实例
目前工作中遇到的问题,是讲项目三大模块拆分, 将Discover.Shop和Events的源代码拆分到独立的项目中 拆分是个麻烦事,里面相互依赖很多 ,打包编译也异常复杂,各种报错 编译Event 遇 ...
- How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?
How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)? 解答 Negative mar ...
- 安装Chrome扩展程序xpath
最近工作用到xpath,直接从浏览器复制下来路径时常会出错而且长度很长,于是我想到之前用过的一款chrome插件,可以直接编写xpath语句,并实时出现解析出的结果,检验xpath语句是否编写正确.效 ...
- linux内核中rtc框架选用什么接口来注册rtc设备呢?
1. 有哪些接口? 1.1 devm_rtc_device_register 1.2 devm_rtc_allocate_device和 rtc_register_device 2. 1.1与1.2 ...
- postgreSQL 之 Privilege & grant & revoke(未完待续)
When an object is created, it is assigned an owner. The owner is normally the role that executed the ...
- 一百三十二:CMS系统之前端动态获取后台添加的轮播图
先准备几张轮播图 排序顺序改为根据优先级倒序排 前端首页接口 @bp.route('/')def index(): banners = BannerModel.query.order_by(Banne ...