24h購物| | PChome| 登入
2013-05-04 14:35:23| 人氣877| 回應0 | 上一篇 | 下一篇

[UVA][math] 10990 - Another New Function

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A
Another New Function
Input: Standard Input

Output: Standard Output

Time Limit: 4 Second

 

The depth of phi value of a number is denoted by the number of steps required before it reaches 1. An example will make it very clear.

 

Φ(13)=12 ...... step 1

Φ(12)=4 ...... step 2

Φ(4)=2 ...... step 3

Φ(2)=1 ...... step 4

 

So the depth of phi(13) is 4. We name this function as depthphi. So we can write depthphi(13)=4. The sum of depthphi function (SODF) takes two integers as parameter and its definition is given below:

Given the value of m and n your job is to find the value of SODF(m,n).

 

Input

The first line of the input file contains an integer N (0<N<2001) which indicates how many sets of inputs are there. Each of the next N lines contains two integers m and n (2 ≤ m ≤ n ≤ 2000000).

 

Output

For each line of input produce one line of output. This line contains an integer S, which actually denotes the value of SODF(m,n).

 

Sample Input                               Output for Sample Input

2
2 10
100000 200000
 

22

1495105

 


Problem setter: Shahriar Manzoor, EPS

Special Thanks: Derek Kisman, EPS

 

The following paragraph is extracted from Mathworld to inform you about phi function.

The totient function phi(n) or phi(n), also called Euler's totient function, is defined as the number of positive integers <=nthat are relatively prime to (i.e., do not contain any factor in common with) n, where 1 is counted as being relatively prime to all numbers. Since a number less than or equal to and relatively prime to a given number is called a totative, the totient function phi(n)can be simply defined as the number of totatives of n. For example, there are eight totatives of 24 (1, 5, 7, 11, 13, 17, 19, and 23), so phi(24)==8. The totient function is implemented in Mathematica as EulerPhi[n].

所謂的 phi 就是與小於等於 n, 且與 n 互質的個數。(數學教過的排容想法去計算之)

接著就按照他的步驟進行計算 depth.

#include <stdio.h>
#define maxL (2000000>>5)+1
#define GET(x) (mark[(x)>>5]>>((x)&31)&1)
#define SET(x) (mark[(x)>>5] |= 1<<((x)&31))
int mark[maxL];
int phi[2000005];
int dep_phi[2000005];
int dp[2000005];
void sieve() {
    register int i, j;
    SET(1);
    int n = 2000000;
    for(i = 2; i <= n; i++) {
        if(!GET(i)) {
            for(j = i; j <= n; j += i) {
                phi[j] = phi[j]/i*(i-1);
                SET(j);
            }
        }
    }
}
int main() {
    int i, j, k;
    for(i = 2; i <= 2000000; i++)
        phi[i] = i;
    sieve();
    for(i = 2; i <= 2000000; i++) {
        dep_phi[i] = 1 + dep_phi[phi[i]];
        dp[i] = dp[i-1] + dep_phi[i];
    }
    scanf("%*d");
    while(scanf("%d %d", &i, &j) == 2)
        printf("%d\n", dp[j]-dp[i-1]);
    return 0;
}

台長: Morris
人氣(877) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][2D String matching][KMP、AC自動機] 11019 - Matrix Matcher
此分類上一篇:[UVA][math] 10174 - Couple-Bachelor-Spinster Numbers.

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文