Farey sequences
n阶的法里数列是0和1之间最简分数的数列,由小至大排列,每个分数的分母不大于n。
Stern-Brocot树(SB Tree)可以生成这个序列
Farey sequences UVA - 10408
求n阶Farey sequences的第k项,找到下一项的递推式,也就是基本不等式
#include <stdio.h>
int main(){
int n,k;
while(~scanf("%d%d",&n,&k)){
int a0=,a1=,b0=,b1=n,a2,b2;
for(int i=;i<k;i++){
int c=(n+b0)/b1;
a2=c*a1-a0;
b2=c*b1-b0;
a0=a1,a1=a2;
b0=b1,b1=b2;
}
printf("%d/%d\n",a1,b1);
}
return ;
}
X - Farey Sequence
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
You task is to calculate the number of terms in the Farey sequence Fn.
Input
Output
Sample Input
2
3
4
5
0
Sample Output
1
3
5
9
这个函数的个数有个近似值,但是这个要求准确的个数,这个个数也没什么规律
Fn是分母是小于n的,而且其他和他互质,欧拉函数是积性函数,所以欧拉函数求下前缀和就行了
E(x)表示比x小的且与x互质的正整数的个数,也就是欧拉函数
#include <stdio.h>
const int N=1e6+;
typedef __int64 ll;
int phi[N],prime[N];
ll sum[N];
bool vis[N];
void Euler()
{
phi[]=;
int cnt=;
for(int i=;i<=1e6;i++)
{
if(!vis[i]){prime[++cnt]=i;phi[i]=i-;}
for(int j=;j<=cnt&&prime[j]*i<=1e6;j++)
{
vis[prime[j]*i]=;
if(i%prime[j])phi[prime[j]*i]=phi[i]*(prime[j]-);
else {phi[prime[j]*i]=phi[i]*prime[j];break;}
}
}
}
int main()
{
Euler();sum[]=;
for(int i=;i<=1e6;i++)
sum[i]+=sum[i-]+phi[i];
int n;
while(~scanf("%d",&n)){
if(!n)break;
printf("%I64d\n",sum[n]);
}
return ;
}
2866: Farey Sequence Again
总提交: 14 测试通过:7
描述
The Farey sequence Fn for any positive integer n is the set of irreducible rational numbers a/b with 0<a<b<=n and (a, b) = 1 arranged in increasing order. Here (a, b) mean the greatest common divisor of a and b. For example:
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
Given two positive integers N and K, with K less than N, you need to find out the K-th smallest element of the Farey sequence FN.
输入
The first line of input is the number of test case T, 1<=T<=1000. Then each test case contains two positive integers N and K. 1<=K<N<=10^9.
输出
For each test case output the Kth smallest element of the Farey sequence FN in a single line.
样例输入
样例输出
题目来源
这个题是真的难啊,想了想查了查相关资料都做不了,最后竟然是利用这个级数增长很快,能互质的1,2,3用完就到1e9了,根本到不了n,贼鸡儿难想,%大佬,TOJ也有高人啊
Updog prepared to enjoy his delicious supper. At the very time he was ready to eat, a serious accident occurred—GtDzx appeared!! GtDzx declared his hadn't eaten anything for 3 days (obviously he was lying) and required Updog to share the cake with him. Further more, he threatened Updog that if Updog refused him, he would delete Updog's account in POJ! Thus Updog had no choice.
Updog intended to cut the cake into s (s ≥ 1) pieces evenly, and then gave t(0≤ t ≤ s) pieces to GtDzx. Apparently GtDzx might get different amount of cake for different s and t. Note that s = 12, t = 4 and s = 6, t = 2 will be regarded as the same case since GtDzx will get equal amount in the two cases. Updog wouldn't separate the cake into more than N pieces.
After sorted all available cases according to the amount of cake for GtDzx, in the first case no cake to gave to GtDzx (t = 0) and in the last case GtDzx would get the whole cake (s = t). Updog wondered that how much cake GtDzx would get in the k-th case.
Input
The first line of the input file contains two integers N (1 ≤ N ≤ 5000) and C(0 ≤ C≤ 3000). The following C lines each contains a positive integer describe C query respectively. The i-th query ki is to ask GtDzx's share of whole cake in the ki-th case .
Output
Answer each query in a separated line, according to the order in the input.
Sample Input
5 4
1
7
11
12
Sample Output
0/1
3/5
1/1
No Solution
这个题也是这个内容,但是也没那么难啊,存一下所有的查询就好了
Farey sequences的更多相关文章
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- POJ 2478 Farey Sequence
名字是法雷数列其实是欧拉phi函数 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)
Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- Python数据类型之“序列概述与基本序列类型(Basic Sequences)”
序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...
- Extract Fasta Sequences Sub Sets by position
cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...
随机推荐
- 解决ajax 遇到session失效后自动跳转的问题
在项目中,经常会遇到session失效后,点击任何链接无反应的情况!这样给客户的体验就不是很好,以为是系统出了故障!所以在项目中我们会处理session失效后的跳转问题(一般给用户提示,并跳转后登录页 ...
- 本号讯 | 微软和百度携手推进全球自动驾驶技术; 微软发布新一代可垂直可水平滚动的Arc鼠标
7 月 13 日,微软宣布了与宝马的最新合作进展,继语音助手 Cortana .云服务 Azure.Office 365 和微软 Exchange 安装在部分宝马车型后——Skype for Busi ...
- 转:error LNK2005: ...already defined in MSVCRTD.lib
编译错误: Error1error LNK2005: _CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.l ...
- ADO.Net——防止SQL注入攻击
规避SQL注入 如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可 ...
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
- Python 继承实现的原理(继承顺序)
继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...
- String java
https://www.golinuxcloud.com/java-interview-questions-answers-experienced-2/ 75. What is the meaning ...
- java基础—java制作证书的工具keytool
一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...
- java基础—网络编程
一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程.
- PhoneGap+JQuery Mobile移动应用开发学习笔记
最近一直在学习使用PhoneGap+JQuery Mobile的开发框架开发Android应用,抛开这个框架的运行效率不说,暂且将使用中遇到的问题进行一下整理. 1.JS文件引用顺序 也许在进行web ...