(Problem 72)Counting fractions
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个元素。
d 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的更多相关文章
- (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 ...
- (Problem 33)Digit canceling fractions
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...
- (Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
- (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 ...
- (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 ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- (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 ...
- (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 ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
随机推荐
- Response.Write具体介绍
问题一: Response.Write 后连接Response.Redirect ,则Response.Write无法显示,直接跳转入Response.Redirect 的页面. 解决方案: Resp ...
- How To Set Dark Theme in Visual Studio 2010
Want to use the visual studio color theme editor to set the dark theme or other themes? Below shows ...
- android UI-EditText的长度监听慎用TextWatcher
在用户昵称的输入时,限定8个字符,本意是在输入超过8个时候,页面toast一个提示,就是下面的TextWatcher的监听,在afterTextChanged中处理. 原bug:huawei MT2- ...
- Filemanager 的使用
filemanager的使用包括: 1.创建文件夹 2.删除文件夹 3.写入文件 4.复制文件 5.移动文件 6.删除文件 一.创建文件夹 首先宏的定义一个字符串作为地址的来获取当前的docum ...
- 建立一个ROS msg and srv
msg是一个描述ROS消息字段的简单的文本文件,它们经常用来为消息产生不同语言的源代码. srv文件描述一个服务,它由请求和响应两部分组成. msg文件被存储在一个包的msg目录下,srv文件被存储在 ...
- Linux学习之chage命令
功能:修改帐号和密码的有效期限用法:chage[-l][-m mindays][-M maxdays][-I inactive][-E expiredate][-W warndays][-d last ...
- Hadoop插件安装
1.首先下载Hadoop对应版本的插件,以Hadoop 1.0版本对应的插件Hadoop-eclipse-plugin1.0.3.jar为例 2.将下载的插件放置到Ecplise安装目录的plugin ...
- 搭建Hadoop集群 (二)
前面的步骤请看 搭建Hadoop集群 (一) 安装Hadoop 解压安装 登录master, 下载解压hadoop 2.6.2压缩包到/home/hm/文件夹. (也可以从主机拖拽或者psftp压缩 ...
- FileInputStream(字节流)与fileReader(字符流) 的区别
FileInputStream 类 1 ) FileInputStream 类介绍: 以字节为单位的流处理.字节序列:二进制数据.与编码无关,不存在乱码问题. FileInputStream 类的主要 ...
- Mysql笔记之 -- replace()实现mysql 替换字符串
mysql 替换函数replace()实现mysql 替换字符串 mysql 替换字符串的实现方法: mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数 ...