hdu 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 找新朋友 (容斥原理 || 欧拉函数)的更多相关文章
- HDU——1286找新朋友(欧拉函数+质数打表)
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 1286 找新朋友 (欧拉函数)
Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的 ...
- hdu 1286 找新朋友(欧拉函数)
题意:欧拉函数 思路:欧拉函数 模板,代码略.
- hdoj 1286 找新朋友【欧拉函数】
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1286:找新朋友(欧拉函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...
- HDU 1286 找新朋友 (欧拉公式或者标记法(其实就是欧拉公式的思想))
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1286 找新朋友 Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 1286:找新朋友(数论,欧拉函数)
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1286 找新朋友 欧拉函数模版题
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- hdu 1286 找新朋友 (欧拉函数)
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- P5562 [Celeste-B]Center of the Earth 题解
构造 因为题目只要求两位相同,所以可以暴力枚举这两位所有的可能性,方案数为\(O(n^2)\). 但是,这么做是显然不优的,因为完全没有用到第三位. 观察题目条件:n为偶数. 就想一想能不能奇数偶数分 ...
- Django项目:CRM(客户关系管理系统)--12--05PerfectCRM实现King_admin注册功能获取内存01
#base_admin.py #Django admin 注册功能的形式 # sites = { # 'crm':{ # 'customers':CustomerAdmin, # 'customerf ...
- H5C3--拖拽接口的使用和实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
在SVN上下载工程项目.使用visual studio打开时,出现如下提示: 查找相关资料,解决办法如下: 使用记事本打开工程目录下的.csproj文件.把<UseIIS>False< ...
- unity如何查找某个脚本挂在了哪些物体上
在开发中往往会遇到一个问题:不知道整个场景中究竟有哪些物体挂载了某一个脚本.如果挨个查找太麻烦了,下面有一种方法可以快速找到解决这个问题. 在unity的Window里有一项Editor tests ...
- Oracle操作XML各种场景介绍
版权声明:凭栏处.潇潇雨歇. https://blog.csdn.net/IndexMan/article/details/28130961 近期在研究Oracle PLSQL中对于XML的系列操作. ...
- 什么? 1XIN = 21BTC
什么? 1XIN = 21BTC 最初看到这个标题,我还回去考证一下. 原来是 Mixin Network 的宣传广告. BTC 是多少? 2100万枚. XIN 是 100 万枚. 所以才有了 1X ...
- pom.xml中若出现jar not found;
pom.xml中若出现jar not found;我们可以直接在view ->tool windows ->Maven Project 中直接install
- HDU 4217
点击打开题目链接 题型就是数据结构.给一个数组,然后又k次操作,每次操作给定一个数ki, 从数组中删除第ki小的数,要求的是k次操作之后被删除的所有的数字的和. 简单的思路就是,用1标记该数没有被删除 ...
- sql —— in
IN 操作符允许我们在 WHERE 子句中规定多个值. 原表: 执行查询: