Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d  8 in ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 21 elements in this set.

How many elements would be contained in the set of reduced proper fractions for d  1,000,000?

题目大意:

考虑分数 n/d, 其中n 和 d 是正整数。如果 nd 并且最大公约数 HCF(n,d)=1, 它被称作一个最简真分数。

如果我们将d  8的最简真分数按照大小的升序列出来,我们得到:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5 , 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

可知该集合中共有21个元素。
 1,000,000的最简真分数集合中包含多少个元素?

算法一(超时):

优化技巧:

1、当分母n是素数,则以n为分母的最简真分数的数目为n-1

2、当分母和分子奇偶不同的时候,进一步检查分母和分子的最大公约数

#include<stdio.h>
#include<stdbool.h>
void swap(int* a, int* b) //交换两值的函数
{
int t;
t = *a;
*a = *b;
*b = t;
} int gcd(int a, int b) //求最大公约数函数
{
int r;
if(a < b) swap(&a, &b);
while(b) {
r = a % b;
a = b;
b = r;
}
return a;
} bool prim(int n) //判断素数函数
{
int i;
for(i = ; i * i <= n; i++) {
if(n % i == ) return false;
}
return true;
} bool fun(int a, int b) //判断两整数奇偶是否相同
{
return !((a & ) & (b & ));
} void solve()
{
int i, j, t;
long long count = ;
for(i = ; i <= ; i++) {
if(i % && prim(i)) {
count += i - ;
continue;
}
count++;
for(j = ; j < i; j++) {
if(fun(i,j) && (i % j != )) {
t = gcd(i, j);
if(t == ) count++; //如果i,j互素,符合
}
}
}
printf("%lld\n", count);
} int main()
{
solve();
return ;
}

算法二:

将1~1000000的整数分奇偶两部分计算,依然超时

#include<stdio.h>
#include<stdbool.h> #define N 1000000 int gcd(int a, int b) //求最大公约数函数
{
int r;
while(b) {
r = a % b;
a = b;
b = r;
}
return a;
} bool prim(int n) //判断素数函数
{
int i;
for(i = ; i * i <= n; i++) {
if(n % i == ) return false;
}
return true;
} bool fun(int a, int b) //判断两整数奇偶是否相同
{
return !((a & ) & (b & ));
} void solve()
{
int i, j, t;
long long count = ;
for(i = ; i <= N; i += ) {
count++;
for(j = ; j < i; j += ) {
t = gcd(i, j);
if(t == ) count++;
} }
for(i = ; i < N; i += ) {
count++;
if(prim(i)) {
count += i - ;
continue;
} else {
for(j = ; j < i; j++) {
if(gcd(i, j)) count++;
}
} }
printf("%lld\n", count);
} int main()
{
solve();
return ;
}

算法三:

#include<stdio.h>
#include<stdbool.h>
#include<math.h> #define N 1000001 bool a[N]; void Eratosthenes()
{
int i, M, j;
for(i = ; i < N; i++) {
a[i] = true;
}
M = (int)sqrt(N);
for(i = ; i <= M; i++) {
if(a[i]) {
j = i * i;
for(; j < N; j += i)
a[j] = false;
}
}
} int fun(int n)
{
int t, i, count, j;
t = n / ;
i = ;
count = n - ;
while(i <= t) {
if(a[i]) {
if(n % i == ) {
count--;
for(j = i; j * i < n; j++) {
count--;
}
}
}
i++;
}
return count;
} int main()
{
int i;
long long count = ; Eratosthenes(); for(i = ; i < N; i++) {
if(a[i]) {
count += i - ;
} else {
count += fun(i);
}
}
printf("%lld\n", count); return ;
}

算法四:使用欧拉函数

//(Problem 72)Counting fractions
// Completed on Tue, 18 Feb 2014, 14:08
// Language: C11
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/ #include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h> #define N 1000001 int phi[N]; //数组中储存每个数的欧拉数 void genPhi(int n)//求出比n小的每一个数的欧拉数(n-1的)
{
int i, j, pNum = ;
memset(phi, , sizeof(phi)) ;
phi[] = ;
for(i = ; i < n; i++)
{
if(!phi[i])
{
for(j = i; j < n; j += i)
{
if(!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i - );
}
}
}
} void solve()
{
int i;
long long ans =;
for(i = ; i < N; i++) {
ans += phi[i];
}
printf("%lld\n", ans);
} int main()
{
genPhi(N);
solve();
return ;
}
Answer:
303963552391

(Problem 72)Counting fractions的更多相关文章

  1. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  2. (Problem 33)Digit canceling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  3. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

  4. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  5. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  6. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  7. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  8. (Problem 46)Goldbach's other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

  9. (Problem 53)Combinatoric selections

    There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...

随机推荐

  1. hdu 2102 A计划(双层BFS)(具体解释)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php ...

  2. android项目的的目录结构

    然后我们看一下Helloword的程序目录: 我们可以看到 大致有的文件: 1. MainHelloWorld.java文件 2. R.java文件 3. android.jar文件 4. RES.L ...

  3. md笔记——HTTP知识

    HTTP权威指南 ******** 第一部分:HTTP:Web 的基础 第一章:HTTP概述 MIME 因特网上有数千种不同的数据类型,HTTP 仔细地给每种要通过 Web 传输的对象都打上了名为 M ...

  4. Java并发编程:Thread类的使用介绍

    在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着介绍Thread类中的方法的具体使用. 以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中 ...

  5. c# session总结

    C# 中对 Session 的“(string)”.“.ToString()”与“Convert.ToString”用法笔记 在实际操作当中,我们经常会遇到将 Session 的值转为 String ...

  6. Infragistics的介绍以及在ASP.net中使用的总结

    Infragistics系列控件是一套很好,很强大的控件,.感觉很好..现在自己做项目也用..却发现网上没有一套中文的教程,中文资料都很少..在这里就把自己的研究心得写下来... 首先安装,一步一步装 ...

  7. Linux学习之挂载操作

    一.挂载U盘 1.使用命令:fdisk -l,查看系统硬盘和分区情况 2.插入优盘,再次用fdisk -l命令查看     大家可以发现多了一个硬盘/dev/sdb和它的一个分区/dev/sdb1 3 ...

  8. JS计算两个日期相差几天

    function Computation(sDate1, sDate2){ var aDate, oDate1, oDate2, iDays aDate = sDate1.split("-& ...

  9. python之面向对象那点事

    一.类与对象 1.什么是类?类,顾名思义,就是具体相同属性的同一类事物的统称及抽象.对象,指是就是具体的抽象类的实例 以上的说法是不是看起来有点云里来雾里去呢.没错,专业的解释总是让人难以理解,这就是 ...

  10. DelphiXe5中的双向绑定(使用使用TBindScope和TBindExpression,并覆盖AfterConstruction函数)

    在Delphi下等这一功能很久了,虽然C#下早已实现了这一功能.但是在Dephi下尝试这项功能时还是有些许的激动.闲言少絮,直接上代码. unit BindingDemo; interface use ...