题目大意:给一个正整数n,求出在[1, n]区间内和n互质的正整数的个数。Euler's Totient(欧拉函数)的直接应用。

 #include <cstdio>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
#define MAXN 10000000 bitset<MAXN+> bs;
vi primes; void sieve(ll upper)
{
bs.set();
bs.set(, false);
bs.set(, false);
for (ll i = ; i <= upper; i++)
{
if (bs.test((size_t)i))
for (ll j = i*i; j <= upper; j++)
bs.set((size_t)j, false);
primes.push_back((int)i);
}
} vi primeFactors(ll n)
{
vi factors;
int idx = , pf = primes[idx];
while (n != && (pf*pf <= n))
{
while (n % pf == )
{
n /= pf;
factors.push_back(pf);
}
pf = primes[++idx];
}
if (n != ) factors.push_back(n);
return factors;
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
sieve(MAXN);
int n;
while (scanf("%d", &n) && n)
{
vi factors = primeFactors(n);
vi::iterator last = unique(factors.begin(), factors.end());
int result = n;
for (vi::iterator it = factors.begin(); it != last; it++)
result = result - result/(*it);
printf("%d\n", result);
}
return ;
}

UVa 10179 - Irreducable Basic Fractions的更多相关文章

  1. 【Uva 12558】 Egyptian Fractions (HARD version) (迭代加深搜,IDA*)

    IDA* 就是iterative deepening(迭代深搜)+A*(启发式搜索) 启发式搜索就是设计估价函数进行的搜索(可以减很多枝哦~) 这题... 理论上可以回溯,但是解答树非常恐怖,深度没有 ...

  2. UVa 10299 - Relatives

    题目大意:Euler's Totient的应用. 几乎和UVa 10179 - Irreducable Basic Fractions一样,于是偷了个懒,直接用10179题的代码,结果WA了,感觉一样 ...

  3. 2021.08.10 Euler函数总结

    2021.08.10 Euler函数总结 知识: 记 φ(n) 表示在 [1,n] 中与 n互质的数的个数. 1.p为质数,则 \[φ(p^l)=p^l-p=p^{l-1}(p-1) \] 注:每p个 ...

  4. 暴力枚举 UVA 10976 Fractions Again?!

    题目传送门 /* x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k */ #include <cstdio> #inc ...

  5. UVA.10986 Fractions Again (经典暴力)

    UVA.10986 Fractions Again (经典暴力) 题意分析 同样只枚举1个,根据条件算出另外一个. 代码总览 #include <iostream> #include &l ...

  6. uva 10976 Fractions Again(简单枚举)

    10976 Fractions Again It is easy to see that for every fraction in the form 1 k (k > 0), we can a ...

  7. Uva 10976 Fractions Again?!

    直接暴力 没技巧 y应该从k+1开始循环,因为不然y-k<0的时候 你相当于(x*y) % (负数) 了. #include <iostream> using namespace s ...

  8. uva 10976 fractions again(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB3gAAAM+CAIAAAB31EfqAAAgAElEQVR4nOzdO7KtPJum69GEpAcVQQ ...

  9. UVa 12558 - Egyptian Fractions (HARD version)

    题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以 ...

随机推荐

  1. PAT (Advanced Level) 1112. Stucked Keyboard (20)

    找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...

  2. dage手法之 头部和banner ad tpl_header

    <div class="top2"> <?php if ($current_page_base == 'index' || $current_page_base ...

  3. Nodejs开源项目里怎么样写测试、CI和代码测试覆盖率

    测试 目前主流的就bdd和tdd,自己查一下差异 推荐 mocha和tape 另外Jasmine也挺有名,angularjs用它,不过挺麻烦的,还有一个选择是qunit,最初是为jquery测试写的, ...

  4. 剑指offer 二叉搜索树后续遍历序列 判断

    最后一个元素是 根节点. 左子树的元素都小于根节点,右子树都大于根节点 然后递归判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  5. Mysql命令-以NULL做where条件过滤时应该写 IS NULL;

    以NULL做where条件过滤时应该写 IS NULL;SELECT * FROM pet WHERE death IS NULL; SELECT * FROM pet WHERE death IS ...

  6. 判断非法字符串的类方法,与jsp

    private String_do_judge judge; if (judge.isContain(key)) { return "feifa"; } 上面这写代码添加到进入ac ...

  7. 【抽屉定理】 组合数学poj2356

    假定n个数为a1,a2,...,an,前n项和分别是S1.S2.....Sn,那么如果有一个Si模n是0,就是答案,否则,n个数模n的余数只能在 1到n - 1之间,把余数作为抽屉,显然n个数放到n ...

  8. createSQLQuery的addEntity跟setResultTransformer方法

    createSQLQuery的addEntity和setResultTransformer方法 1. 使用SQLQuery对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Sessi ...

  9. CATranstion动画

    // 1.创建过度动画 CATransition *anima = [CATransition animation]; // 2.设置动画类型 anima.type = @"cube&quo ...

  10. ARM汇编指令集

    一.跳转指令.跳转指令用于实现程序流程的跳转,在ARM程序中有以下两种方法可以实现程序流程的跳转. Ⅰ.使用专门的跳转指令.Ⅱ.直接向程序计数器PC写入跳转地址值. 通过向程序计数器PC写入跳转地址值 ...