Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)
Relatively Prime Powers
Consider some positive integer xx. Its prime factorization will be of form x=2k1⋅3k2⋅5k3⋅…x=2k1⋅3k2⋅5k3⋅…
Let's call xx elegant if the greatest common divisor of the sequence k1,k2,…k1,k2,… is equal to 11. For example, numbers 5=515=51, 12=22⋅312=22⋅3, 72=23⋅3272=23⋅32 are elegant and numbers 8=238=23 (GCD=3GCD=3), 2500=22⋅542500=22⋅54 (GCD=2GCD=2) are not.
Count the number of elegant integers from 22 to nn.
Each testcase contains several values of nn, for each of them you are required to solve the problem separately.
Input
The first line contains a single integer TT (1≤T≤1051≤T≤105) — the number of values of nn in the testcase.
Each of the next TT lines contains a single integer nini (2≤ni≤10182≤ni≤1018).
Output
Print TT lines — the ii-th line should contain the number of elegant numbers from 22to nini.
Example
Input
4427210
Output
21616
Note
Here is the list of non-elegant numbers up to 1010:
- 4=22,GCD=24=22,GCD=2;
- 8=23,GCD=38=23,GCD=3;
- 9=32,GCD=29=32,GCD=2.
The rest have GCD=1GCD=1.
题意:
给你一个大于等于2的整数N
让你求2~N 中有多少个整数x,
唯一分解后质因子的幂次分别是e1,e2,e3, 时 gcd(e1,e2,e3)=1
思路:
正难则反,一共有N-1个数,我们只需要减去那些gcd不为1的即可,
我们可以分别枚举gcd为2,3,4,5.,,,, 等等
根据容斥原理,gcd 为i时,他对答案的贡献即为 mu[i]*(n^(1/i) -1 ) mu是莫比乌斯函数。
至于系数为什么恰好是莫比乌斯函数,可以先学这篇博客感受一下:
https://www.cnblogs.com/qieqiemin/p/11537681.html
那么我们来看n^(1/i) -1 是2~n中,质因子分解幂次都为i的数的个数。
即n开i次方-1,先去的1就是就是一个数开任何次方都>=1,数字1被算进去了,需要减去。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
long long gen(long long n, long long k)
{
long long t = powl(n, 1. / k) - 0.5;
return t + (powl(t + 1, k) - 0.5 <= n);
}
#define N maxn
bool vis[N];
long long prim[N], mu[N], sum[N], cnt;
void get_mu(long long n)
{
mu[1] = 1;
for (long long i = 2; i <= n; i++) {
if (!vis[i]) {mu[i] = -1; prim[++cnt] = i;}
for (long long j = 1; j <= cnt && i * prim[j] <= n; j++) {
vis[i * prim[j]] = 1;
if (i % prim[j] == 0) { break; }
else { mu[i * prim[j]] = -mu[i]; }
}
}
for (long long i = 1; i <= n; i++) { sum[i] = sum[i - 1] + mu[i]; }
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int t;
get_mu(maxn - 1);
du1(t);
while (t--) {
ll n;
scanf("%lld", &n);
ll ans = n - 1;
for (ll i = 2ll; i <= 64ll; ++i) {
ans += mu[i] * (gen(n, i) - 1ll);
}
printf("%lld\n", ans );
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)的更多相关文章
- HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法
题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...
- Tmutarakan Exams URAL - 1091(莫比乌斯函数 || 容斥)
题意: 求1 - s 中 找出k个数 使它们的gcd > 1 求这样的k个数的对数 解析: 从每个素数的倍数中取k个数 求方案数 然后素数组合,容斥一下重的 奇加偶减 莫比乌斯函数的直接套模 ...
- BZOJ 2440 莫比乌斯函数+容斥+二分
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5473 Solved: 2679[Submit][Sta ...
- F - Tmutarakan Exams URAL - 1091 -莫比乌斯函数-容斥 or DP计数
F - Tmutarakan Exams 题意 : 从 < = S 的 数 中 选 出 K 个 不 同 的 数 并 且 gcd > 1 .求方案数. 思路 :记 录 一 下 每 个 数 的 ...
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- 完全平方数 HYSBZ - 2440 (莫比乌斯函数容斥)
完全平方数 HYSBZ - 2440 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些 数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而 这丝毫不影响他对其他 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- bzoj2440 完全平方数 莫比乌斯值+容斥+二分
莫比乌斯值+容斥+二分 /** 题目:bzoj2440 完全平方数 链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2440 题意:求第k个小x数 ...
- hdu1695(莫比乌斯)或欧拉函数+容斥
题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...
随机推荐
- Linux下中文乱码
Linux下中文乱码 修改mysql配置文件,centeros下 配置文件在 /etc/my.cnf vi /etc/my.cnf 在[mysqld]段下添加 character-set-server ...
- iscsi-文件类型
iSCSI简介(Internet SCSI): iSCSI 小型计算机系统接口,IBM公司研发,用于在IP网络上运行SCSI协议:解决了 SCSI需要直连存储设备的局限性:可以不停机扩展存储容量,iS ...
- maven运行工程
1.cd到工程目录下,执行打包命令-----mvn package 2.cd到工程的target目录执行运行命令 java -classpath myapp-1.0-SNAPSHOT.jar cn.m ...
- SGI RB-tree深入理解
前言 在学习STL源码之前我也曾无数次想要弄懂红黑数的原理,奈何每次都被困难打退.说实话,红黑树是真的很难理解,需要不断沉淀才能慢慢体会其妙处.这两天看SGI的RB-tree实现,结合侯捷老师的< ...
- 迭代器iterator和traits编程技法
前言 这段时间研读SGI-STL-v2.91源码,并提炼核心代码自己实现一遍,感觉受益颇深.觉得有必要写一些文章记录下学习过程的思考,行文旨在总结,会大量参考侯捷<STL源码剖析>的内容. ...
- linux 静态库 ar命令用法
当我们的程序中有经常使用的模块,而且这种模块在其他程序中也会用到,这时按照软件重用的思想,我们应该将它们生成库,使得以后编程可以减少开发代码量.这里介绍命令ar,用来对库操作. 1.ar基本用法 ar ...
- Hadoop集群搭建-05安装配置YARN
Hadoop集群搭建-04安装配置HDFS Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hadoop集群搭建-01前期准备 先保证集群5台虚 ...
- MFC控件使用大全
https://blog.csdn.net/daoming1112/article/details/54698113
- c++学习笔记之函数重载和模板理解
1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...
- Selenium IDE for firefox
第一次接触selenium. 首先, selenium支持的 Firefox版本是17.0~34.x. 打开火狐历史版本:http://ftp.mozilla.org/pub/firefox/rele ...