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. Python 数据文件操作——写出数据

  2. 洛谷P1470 最长前缀

    P1470 最长前缀 Longest Prefix 题目描述 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 ...

  3. golang学习资料必备

    核心资料库 https://github.com/yangwenmai/learning-golang

  4. Go开发 之 Go如何引用github包

  5. Django项目:CRM(客户关系管理系统)--02--01PerfectCRM基本配置ADMIN02

    三.CRM项目表结构设计 from django.db import models # Create your models here. """ #运行 Terminal ...

  6. android中的http框架,使其更加简单易用

    Afinal 是一个android的sqlite orm 和 ioc 框架. Afinal 是一个android的sqlite orm 和 ioc 框架.同时封装了android中的http框架,使其 ...

  7. Leetcode884.Uncommon Words from Two Sentences两句话中的不常见单词

    给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不常用单 ...

  8. input 手机数字键盘

    要一点击提起数字键盘,安卓只要设置input的类型是number或tel, ios 需要 pattern="number"可以直接打开搜狗输入法的数字键盘,可以输入.和数字如果只能 ...

  9. PHP开发api接口安全验证的实例,值得一看

    php的api接口 在实际工作中,使用PHP写api接口是经常做的,PHP写好接口后,前台就可以通过链接获取接口提供的数据,而返回的数据一般分为两种情况,xml和json,在这个过程中,服务器并不知道 ...

  10. 微信小程序 —— wepy 使用 Vant Weapp

    一.下载 npm i @vant/weapp -S --production 下载完毕之后,就可以在 node_modules 文件夹里,看见下载的包了. 2.移动文件夹 把刚刚下载的包文件夹下的 l ...