您的位置:首页 > 其它

hdu 1286 找新朋友 (容斥原理 || 欧拉函数)

2013-07-04 01:40 316 查看
Problem - 1286

用容斥原理做的代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

const int N = 33333;

int last
;
void pre() {
last[1] = 1;
for (int i = 2; 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 = 1;
while (n > 1) {
int tmp = last
;
//        cout << tmp << endl;
ret *= tmp - 1;
n /= last
;
while (tmp == last
) {
ret *= tmp;
n /= last
;
}
}
return ret;
}

int main() {
pre();
int T, n;
cin >> T;
while (T-- && cin >> n) cout << phi(n) << endl;
return 0;
}


View Code

——written by Lyon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: