Problem - 1286

用容斥原理做的代码:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector> using namespace std; const int N = ; int last[N];
void pre() {
last[] = ;
for (int i = ; i < N; i++) {
if (!last[i]) {
for (int j = i; j < N; j += i) {
last[j] = i;
}
}
}
// for (int i = 0; i < 20; i++) cout << last[i] << endl;
} vector<int> fac;
void getFac(int n) {
fac.clear();
while (n > ) {
fac.push_back(last[n]);
n /= last[n];
}
int t = (int) (unique(fac.begin(), fac.end()) - fac.begin());
while (fac.size() > t) fac.pop_back();
} int cntBit(int n) { return n > ? cntBit(n >> ) + (n & ) : ;} int main() {
pre();
int T, n;
cin >> T;
while (T-- && cin >> n) {
getFac(n);
int ans = ;
for (int i = , szi = << fac.size(); i < szi; i++) {
int tmp = ;
for (int j = , szj = fac.size(); j < szj; j++) {
if (i & << j) tmp *= fac[j];
}
if (cntBit(i) & ) {
ans += n / tmp;
} else {
ans -= n / tmp;
}
}
cout << n - ans << endl;
}
return ;
}

用欧拉函数做的代码:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector> using namespace std; const int N = ; int last[N];
void pre() {
last[] = ;
for (int i = ; i < N; i++) {
if (!last[i]) {
for (int j = i; j < N; j += i) {
last[j] = i;
}
}
}
// for (int i = 0; i < 20; i++) cout << last[i] << endl;
} int phi(int n) {
int ret = ;
while (n > ) {
int tmp = last[n];
// cout << tmp << endl;
ret *= tmp - ;
n /= last[n];
while (tmp == last[n]) {
ret *= tmp;
n /= last[n];
}
}
return ret;
} int main() {
pre();
int T, n;
cin >> T;
while (T-- && cin >> n) cout << phi(n) << endl;
return ;
}

——written by Lyon

hdu 1286 找新朋友 (容斥原理 || 欧拉函数)的更多相关文章

  1. HDU——1286找新朋友(欧拉函数+质数打表)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. hdu 1286 找新朋友 (欧拉函数)

    Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的 ...

  3. hdu 1286 找新朋友(欧拉函数)

    题意:欧拉函数 思路:欧拉函数 模板,代码略.

  4. hdoj 1286 找新朋友【欧拉函数】

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. HDU 1286:找新朋友(欧拉函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...

  6. HDU 1286 找新朋友 (欧拉公式或者标记法(其实就是欧拉公式的思想))

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1286 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. hdu 1286:找新朋友(数论,欧拉函数)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. hdu 1286 找新朋友 欧拉函数模版题

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  9. hdu 1286 找新朋友 (欧拉函数)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Leetcode24.Swap Nodes in Pairs两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  2. 模拟19 题解(waiting)

    T1,千万别转化成链了!! 直接数就可以,dfs搜索每种情况,对于搜到的点,如果子树大小过大,直接return,相等说明可以,小的话向上累加, 优化是先预处理子树大小,若子树小,不用搜了直接加上就行 ...

  3. 提交方式get和post有什么区别

    提交方式post和get有什么区别? (1)post是向服务器传送数据:get是从服务器上获取数据. (2)在客户端,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个 ...

  4. 杨柳絮-Info:阜阳市多举措治理杨柳絮问题

    ylbtech-杨柳絮-Info:阜阳市多举措治理杨柳絮问题 1.返回顶部 1. 阜阳市多举措治理杨柳絮问题 2019-4-15 10:34| 发布者: 戴斐 | 查看: 56407| 评论: 0|原 ...

  5. 基于mcp940反编译Minecraft源代码

    引言 Minecraft中文叫"我的世界",没怎么深入玩过,来试试把它源代码反编译出来吧. 参考教程: https://minecraft.gamepedia.com/Mods/C ...

  6. 配置了两天python【python可以的】

    首先是看cs231n 发现代码的版本是py2 而我只装了 py3(anaconda3) 怎么办呢 于是想办法装了 anaconda2 并与之共存 ,调用的时候用 activate py2调用 http ...

  7. PHP核心编程--目录操作(包含文件操作)

    一.目录操作 1.    创建目录 创建目录mkdir(目录名称)  //返回值为布尔 一次性创建主目录及其子目录 判断是不是一个目录is_dir  //返回true,false 判断目录或文件是否存 ...

  8. 【linux】Ubuntu16.04中文输入法安装

    最近刚给笔记本装了Ubuntu+win10双系统,但是ubuntu16.04没有自带中文输入法,所以经过网上的一些经验搜索整合,分享一下安装中文输入法的心得.本文主要介绍了谷歌拼音跟ibus中文输入法 ...

  9. Spring+Stomp+ActiveMq实现websocket长连接

    stomp.js+spring+sockjs+activemq实现websocket长连接,使用java配置. pom.xml(只列出除了spring基本依赖意外的依赖,spring-version为 ...

  10. thrift python安装

    http://thrift.apache.org/download/ tar -zxvf thrift-0.11.0.tar.gz cd thrift-0.11.0 ./configure --pre ...