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. day38 08-Spring的id、name和scope顺序

    访问的路径的是/login.id不允许出现特殊的字符./是特殊的字符.Struts 2已经没有/,action的名字已经不带/了.现在的开发中一般使用id这个属性即可. 这个类在被Spring创建的时 ...

  2. 实践中了解到的CSS样式的优先级

    CSS三大特性——继承.优先级和层叠.这是在精通CSS中重点强调的内容. 继承即子类元素继承父类的样式,常用的可继承样式有:color,font,line-height,list-style,text ...

  3. 双系统删除ubuntu

    我的电脑安装了双系统,Windows和Linux,不过由于Linux在最近一段时间内不会使用,所以我打算删除Linux.    删除Linux最需要注意的地方,就是MBR(Master Boot Re ...

  4. 二维码识别项目zxing横屏改为竖屏

    第1步: 在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改: android:screenOrientation="por ...

  5. web标准中定义id与class有什么区别吗

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/x ...

  6. String int 变量互相转化

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  7. 2019-8-30-win10-uwp-好看的时间选择控件

    title author date CreateTime categories win10 uwp 好看的时间选择控件 lindexi 2019-08-30 08:57:20 +0800 2018-0 ...

  8. CentOS 上MySQL报错Can't connect to local Mysql server through socket '/tmp/mysql.scok' (111)

    好吧,这是最常见的MySQL服务没有打开 那就赶紧去打开啊! 在管理员模式下运行以下语句: /usr/local/mysql/bin/mysqld_safe --user=mysql & 成功 ...

  9. span元素和div元素的浮动效果

    首先看一段代码: <style> #right {margin: 10px;float:right;color:red;} #left {float:left;color:blue;} & ...

  10. 大数据技术之HBase

    第1章 HBase简介 1.1 什么是HBase HBase的原型是Google的BigTable论文,受到了该论文思想的启发,目前作为Hadoop的子项目来开发维护,用于支持结构化的数据存储. 官方 ...