HDU 3970 Harmonious Set 容斥欧拉函数
题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970
给定n
。。。并不会怎样递推。
。
思路:
然后这是公式: q=2%2C2%2C4%2C4%2C8%2C12%2C20%2C32%2C60&language=english&go=Search">点击打开链接
a(n) = 1/n * sum_{d divides n and d is odd} 2^(n/d) * phi(d).
d最大是n,也就是1e9,要计算1e9的phi,所以容斥来算phi.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <vector>
#include <string>
#include <time.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <set>
#include <map>
const int inf = 1e9;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9) pt(x / 10);
putchar(x % 10 + '0');
}
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> pii;
int Pow(int x, int y){//高速幂
int ans = 1;
while (y){
if (y & 1)ans = (ll)ans*x%mod;
y >>= 1;
x = (ll)x*x%mod;
}
return ans;
}
int prime[N], primenum;//素数表
void PRIME(int Max_Prime){
primenum = 0;
prime[primenum++] = 2;
for (int i = 3; i <= Max_Prime; i += 2)
for (int j = 0; j<primenum; j++)
if (i%prime[j] == 0)break;
else if (prime[j]>sqrt((double)i) || j == primenum - 1)
{
prime[primenum++] = i;
break;
}
}
void add(int &x, int y){ x += y; if (x >= mod)x -= mod; }//加法
void go(int x, vector<int>&Pri, vector<int>&Num){//分解质因数
Pri.clear(); Num.clear();
while (!(x & 1))x >>= 1;
for (int i = 1; (ll)prime[i] * prime[i] <= x; i++){
if (x%prime[i])continue;
Pri.push_back(prime[i]);
Num.push_back(0);
while (x%prime[i] == 0)x /= prime[i], Num[Num.size() - 1]++;
}
if (x != 1 && (x&1))Pri.push_back(x), Num.push_back(1);
}
vector<int>_pri, _num;
void cal(int id, int mul, int siz, int sor, int &now){//容斥算欧拉函数
if (id == _pri.size()){
if (mul == 1)return;
if (siz & 1)now += sor / mul;
else now -= sor / mul;
return;
}
cal(id + 1, mul, siz, sor, now);
cal(id + 1, mul*_pri[id], siz + 1, sor, now);
}
int phi(int x){
if (x == 1)return 1;
go(x, _pri, _num);
int now = 0;
cal(0, 1, 0, x, now);
return x - now;
}
int ans, n;
vector<int>pri, num;
void dfs(int id, int d){
if (id == pri.size())
{
add(ans, (ll)Pow(2, n / d) * phi(d) % mod);
return;
}
for (int i = 0; i <= num[id]; i++){
dfs(id + 1, d);
d *= pri[id];
}
}
int main(){
PRIME(1e5);
int T; rd(T);
while (T--){
rd(n);
go(n, pri, num);
ans = 0;
dfs(0, 1);
pt((ll)ans*Pow(n, mod - 2) % mod); puts("");
}
return 0;
}
HDU 3970 Harmonious Set 容斥欧拉函数的更多相关文章
- hdu 1695 GCD 容斥+欧拉函数
题目链接 求 $ x\in[1, a] , y \in [1, b] $ 内 \(gcd(x, y) = k\)的(x, y)的对数. 问题等价于$ x\in[1, a/k] , y \in [1, ...
- hdu 1286 找新朋友 (欧拉函数)
Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的 ...
- hdu 4983 Goffi and GCD(欧拉函数)
Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...
- HDU 6322.Problem D. Euler Function -欧拉函数水题(假的数论题 ̄▽ ̄) (2018 Multi-University Training Contest 3 1004)
6322.Problem D. Euler Function 题意就是找欧拉函数为合数的第n个数是什么. 欧拉函数从1到50打个表,发现规律,然后勇敢的水一下就过了. 官方题解: 代码: //1004 ...
- HDU——1286找新朋友(欧拉函数+质数打表)
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 1286 找新朋友(欧拉函数)
题意:欧拉函数 思路:欧拉函数 模板,代码略.
- HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解
题意: 给定\(n,m,p\),求 \[\sum_{a=1}^n\sum_{b=1}^m\frac{\varphi(ab)}{\varphi(a)\varphi(b)}\mod p \] 思路: 由欧 ...
- hdu 2824 The Euler function(欧拉函数)
如果打表的话会超内存,我想到了一种方法解决这个问题.题目给出的数据时3000000,我将三百万分成300个数据,将整万的数据存储下来,计算的时候,先计算x和y之间整万的数据,然后再计算零散数据. 想法 ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
随机推荐
- #pragma pack(push) 和#pragma pack(pop) 以及#pragma pack()
我们知道结构体内存对齐字节可以通过#pragma pack(n) 的方式来指定. 但是,有没有想过一个问题,某些时候我想4字节对齐,有些时候我又想1字节或者8字节对齐,那么怎么解决这个问题呢? 此时, ...
- Mysql 多表查询详解
Reference: https://blog.csdn.net/jintao_ma/article/details/51260458 一.前言 二.示例 三.注意事项 一.前言 上篇讲到Mysql ...
- 网络构建入门技术(1)——IP概述
说明(2017-5-10 11:02:31): 0. IP地址的作用,TCP/IP,网络层,包,源IP(哪个设备发出).目的IP(数据要发到哪个设备),每个设备需要唯一的一个IP地址.32个0到32个 ...
- Hydra扫描姿势
参数详解: -R 根据上一次进度继续破解 -S 使用SSL协议连接 -s 指定端口 -l 指定用户名 -L 指定用户名字典(文件) -p 指定密码破解 -P 指定密码字典(文件) -e 空密码探测和指 ...
- [转]python进行中文文本聚类(切词以及Kmeans聚类)
简介 查看百度搜索中文文本聚类我失望的发现,网上竟然没有一个完整的关于Python实现的中文文本聚类(乃至搜索关键词python 中文文本聚类也是如此),网上大部分是关于文本聚类的Kmeans聚类的原 ...
- C#中的Partial
Partial关键词定义的类可以在多个地方被定义,最后编译的时候会被当作一个类来处理. 首先看一段在C#中经常出现的代码,界面和后台分离,但是类名相同. public partial class Fo ...
- Spring高级装配(一) profile
Spring高级装配要学习的内容包括: Spring profile 条件化的bean声明 自动装配与歧义性 bean的作用域 Spring表达式语言 以上属于高级一点的bean装配技术,如果你没有啥 ...
- Linxu磁盘分区
http://vbird.dic.ksu.edu.tw/linux_basic/0130designlinux.php#hardware_know(好文章) http://blog.chinaunix ...
- SpringMVC @SessionAttributes 使用详解以及源码分析
@sessionattributes @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Docum ...
- Quartz Scheduler Calendar日历的使用
Quartz Calendar 日历的使用 quartz引擎为我们提供了日历的功能,让我们可以自己定义一个时间段,可以控制触发器在这个时间段内触发或者不触发,比如可以设置节假日,工作时间早8晚5等等. ...