求逆欧拉函数(arc)
已知欧拉函数计算公式
初始公式:φ(n)=n*(1-1/p1)*(1-1/p2).....*(1-1/pm)
又 n=p1^a1*p2^a2*...*ps^as 欧拉函数是积性函数
那么:φ(n)=φ(p1^a1)* φ(p2^a2)........φ(pn^an).
#include<cstdio>
#include<ctime>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define maxn 100010
#define MAXN 10000000
using namespace std;
LL n,k,prime[MAXN/],cnt,ans[maxn];
bool mark[MAXN+];
void first()
{
for(int i=;i<=MAXN;i++)
{
if(!mark[i]) prime[++cnt]=i;
for(int j=;j<=cnt;j++)
{
if(i*prime[j]>MAXN) break;
mark[i*prime[j]]=;
if(i%prime[j] ==) break;
}
}
return ;
}
LL mul(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&) ans=(ans+a)%mod;
a=(a<<)%mod;b>>=;
}
return ans;
}
LL low(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&) ans=mul(ans,a,mod);
b>>=,a=mul(a,a,mod);
}
return ans;
}
bool Is_prime(LL n)
{
if(n<) return ;
if(n==) return ;
if(n% ==) return ;
LL m=n-,j=;
while(m% == )
{
j++;
m>>=;
}
for(int i=;i<=;i++)
{
LL a=rand()%(n-)+;
LL x=low(a,m,n);
LL y;
for(int k=;k<=j;k++)
{
y=mul(x,x,n);
if(y==&&x!=&&x!=n- )
return ;
x=y;
}
if(x!=) return ;
}
return ;
}
void dfs(LL x,LL y,LL mod )
{
if(x==)
{
ans[++ans[]]=y;
return ;
}
if(x+ > prime[cnt] && Is_prime(x+))
ans[++ans[]]=y*(x+);
for(int i=mod;i>=;i--)
if(x%(prime[i]-) == )
{
LL a=x/(prime[i]-),b=y,c=;
while(a%c ==)
{
b*=prime[i];
dfs(a/c,b,i-);
c*=prime[i];
}
}
}
int main()
{
freopen("arc.in","r",stdin);
freopen("arc.out","w",stdout);
scanf("%lld%lld",&n,&k);
srand(time());
first();
dfs(n,,cnt);
sort(ans+,ans++ans[]);
for(int i=;i<=k;i++)
printf("%lld ",ans[i]);
return ;
}
求逆欧拉函数(arc)的更多相关文章
- √n求单值欧拉函数
基本定理: 首先看一下核心代码: 核心代码 原理解析: 当初我看不懂这段代码,主要有这么几个问题: 1.定理里面不是一开始写了一个n*xxx么?为什么代码里没有*n? 2.ans不是*(prime[i ...
- 【BZOJ4803】逆欧拉函数
[BZOJ4803]逆欧拉函数 题面 bzoj 题解 题目是给定你\(\varphi(n)\)要求前\(k\)小的\(n\). 设\(n=\prod_{i=1}^k{p_i}^{c_i}\) 则\(\ ...
- O(n)求素数,求欧拉函数,求莫比乌斯函数,求对mod的逆元,各种求
筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1, ...
- (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)
题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- POJ 2480 (约数+欧拉函数)
题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...
- Bzoj-2818 Gcd 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...
- hdu 2824 The Euler function(欧拉函数)
题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...
- UVA 10214 Trees in a Wood(欧拉函数)
题意:给你a.b(a<=2000,b<=2000000),问你从原点可以看到范围在(-a<=x<=a,-b<=y<=b)内整数点的个数 题解:首先只需要计算第一象限 ...
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
随机推荐
- HihoCoder1673 : 01间隔矩阵([Offer收割]编程练习赛41)(单调队列)
描述 给定一个N × M的01矩阵,小Hi希望从中找到一个01间隔的子矩阵,并且子矩阵的面积越大越好. 例如对于 0101010 1000101 0101010 1010101 0101010 在右侧 ...
- SoundHound Inc. Programming Contest 2018
A - F Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are give ...
- scala & spark实战
java.lang.Long is not a valid external type for schema of string java.lang.RuntimeException: Error ...
- mysql错误-修改mysql.sock位置
在Mysql下有时候会出现mysql.sock位置错误,导致无法链接数据库. mac下报错的时候: 首先修改my.cnf 位置在/etc/my.cnf下,假如没有的话,去/usr/locate/mys ...
- cassandra根据用户名密码登录cqlsh
修改conf目录下cassandra.yaml文件 authenticator: PasswordAuthenticator //将authenticator修改为PasswordAuthentic ...
- 三层架构与MVC比较:
三层架构与MVC比较: 1.两者不是同一概念 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. MVC是一个设计模式,它是根据项目的具体需求来决定是否适用于该项目. 那么架构跟设计模式 ...
- 将eclipse java程序打包成jar的总结(包括工程中没有引用外部jar包和有引用外部jar包两种情况)
一.当eclispe java工程中没有引用外部jar包时: 选中工程---->右键,Export...--->Java--->JAR file--->next-->填写 ...
- CF-851B
B. Arpa and an exam about geometry time limit per test 2 seconds memory limit per test 256 megabytes ...
- Nginx的一些优化(突破十万并发)
Nginx的一些优化(突破十万并发) nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_a ...
- QListWidget笔记
1.头文件:#include <QListWidget> 2.继承自:QListView 3.基本代码: #include "mainwindow.h" #includ ...