1. 程式人生 > >2478 Farey Sequence(phi打表)

2478 Farey Sequence(phi打表)

題目:

Description

The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are 
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

There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 10 6). There are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn. 

Sample Input

2
3
4
5
0

Sample Output

1
3
5
9

這個題目無非就是求尤拉函式的前n項和唄

因為查詢不知道有多少個,所以要先打表。

程式碼:

#include<iostream>
#include<stdio.h>
using namespace std;

long long phi[1000001];		//前i個數的phi之和

void get_phi()
{
	for (int i = 1; i <= 1000000; i++)phi[i] = i;
	for (int i = 2; i <= 1000000; i++)
	{
	if (phi[i] ==
i)for (int j = i; j <= 1000000; j += i)phi[j] = phi[j] / i*(i - 1); phi[i] += phi[i - 1]; //去掉這一行就是求尤拉函式 } } int main() { get_phi(); int n; while (scanf("%d",&n)) { if (n == 0)break; printf("%llu\n", phi[n] - 1); } return 0; }

相關推薦

2478 Farey Sequencephi

題目: Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible

poj 2478 Farey Sequence尤拉函式

Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13204 Accepted: 5181 Description The Farey Sequence Fn fo

Lucky Sum dfs

tor 精華 val put pan express only lld esp Lucky Sum Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers wh

CodeForces - 271B Prime Matrix 素數

You've got an n × m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an

3520: Prime Palindromes 素數

3520: Prime Palindromes  時間限制(普通/Java):1000MS/3000MS     記憶體限制:65536KByte 描述 The number 151 is a prime palindrome because i

NOIP 2018模擬賽數學 2018 11 3 B組 Day1 T1

1.傅立葉(fft.c/fft.cpp/fft.pas) 【題目背景】 std最近在學校基房學到了著名的快速傅立葉變換(FFT)。 然而std太弱了,調一道模板題還調了兩個小時······ 然後,他發現,課後例題第一道他就不會做······ 於是他找到了已經AK了N

271B Prime Matrix 素數

You've got an n × m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an

Farey Sequence歐拉函數

公式 log function include string.h -1 mod eof 歐拉 題意:給出式子F F中分子分母互質,且分子小於分母 例: F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2

Goldbach`s Conjecture LightOJ - 1259 素數 哥德巴赫猜想

rim inf clu 就是 include str cst name long long 題意: 就是哥德巴赫猜想。。。任意一個偶數 都可以分解成兩個(就是一對啦)質數的加和 輸入一個偶數求有幾對。。 解析: 首先! 素數打表。。因為 質數 + 質數 = 偶數 所以 偶數

HDU-1016 Prime Ring ProblemDFS深搜+

inf rap problem pac 如果 pos using 所有 get 題目回顧(HDU-1016): Prime Ring Problem Problem Description A ring is compose of n circles as shown

只包含因子2 3 5的數51nod 思維

K的因子中只包含2 3 5。滿足條件的前10個數是:2,3,4,5,6,8,9,10,12,15。 所有這樣的K組成了一個序列S,現在給出一個數n,求S中 >= 給定數的最小的數。 例如:n = 13,S中 >= 13的最小的數是15,所以輸出15。 Input 第1行:一個數T

Tournament ZOJ - 4063 青島區域賽 F

打表題。。 規律是找出來了 奈何優化不了 。。。。 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #in

【CodeForces - 244B】Undoubtedly Lucky Numbers dfs + 二分

題幹: Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes

【Undoubtedly Lucky Numbers】【CodeForces - 244B】dfs+暴力

題目:   Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading

K - JiLi Number

思路:總共到100000000000有 83個數,所以打表列出很方便。(一開始用大數做了好久,超時) #include<iostream> #include<cstdio> #include<cstring> using namespace std; t

遞推牛客練習賽25 C再編號

連結:https://www.nowcoder.com/acm/contest/158/C 來源:牛客網   時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 262144K,其他語言524288K 64bit IO Format: %lld 題目描述 n 個人,

***uva1225 數數字

題目大意: 把前n(n<=10000) 個整數按順序寫在一起,123456789101112···· 數一數0~9個出現多少次(輸出10個數,分別是按0,1,2,3····9出現的次數) #include <iostream> #inclu

Newcoder 156 B.托米的劃分

Description 歐洲人托米非常喜歡數字,他經常在空閒時玩下面的遊戲 對於一個數字 nnn, 托米會隨性選中一個數 ppp,$ (1< p \le n),將, 將,將 n $拆分成 u=

JiLi Number

思路:總共到100000000000有 83個數,所以打表列出很方便。(一開始用大數做了好久,超時) #include<iostream> #include<cstdio> #include<cstring> using namesp

244B】Undoubtedly Lucky Numbers dfs + 二分

題幹: Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leadi