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 ...
随机推荐
- hdu 4722 Good Numbers( 数位dp入门)
Good Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- JDBC vs Hibernate(转)
jdbc和Hibernate区别 刚开始学习JAVA时,认为Hibernate是一个很神圣的东西,好像是会了SSH,就能走遍全世界一样.记得曾经在枫叶面试的时候,我们几个同学出还说这个公司怎么这么的落 ...
- intellij idea cpu占用率太大太满 运行速度太慢解决方案
1.关掉代码检查. setting -> Editor -> Inspections,把右面方框框里的对勾全去掉,或者根据需要去掉.原理是关闭不必要的代码检查,提高速度. 2.关掉多余的插 ...
- 学习String类
1. 描述: String类是java中比较常用的类, 表示字符串类型 当拼接大量数据时, String类性能没有StringBuilder和StringBuffer性能高 2. 常用的String语 ...
- Nginx 对访问量的控制
目的 了解 Nginx 的 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块,对请求访问量进行控制. Nginx 模块化 nginx ...
- facebook登录深入研究
PHP sdk https://developers.facebook.com/docs/php/gettingstarted javascript对接PHP https://developers.f ...
- Amazon Redshift数据迁移到MaxCompute
Amazon Redshift数据迁移到MaxCompute Amazon Redshift 中的数据迁移到MaxCompute中经常需要先卸载到S3中,再到阿里云对象存储OSS中,大数据计算服务Ma ...
- ios开发ARC,IBOutlets之strong与weak
今天在写程序的时候,用IBOutlets连了一个自定义的控件,出现了问题,后面访问的时候,控件里有些subviews没有初始化好,取到的时候为nil, 程序里用了ARC, IBOutlets一连接上, ...
- 纯CSS3个性化圆形按钮登录表单
在线演示 本地下载
- objectarx之画多段线和画直线
void CCommonFuntion::DrowPloyLine(AcGePoint2dArray& inputpoints){ if (inputpoints.length() < ...