24h購物| | PChome| 登入
2012-09-29 22:17:12| 人氣1,369| 回應0 | 上一篇 | 下一篇

[UVA][二分搜] 11876 - N + NOD (N)

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

I

N + NOD (N)

Input

Standard Input

Output

Standard Output

 

Consider an integer sequence N where,

N0 = 1
Ni = Ni-1 + NOD(Ni-1)- for i > 0

Here, NOD(x) = number of divisors of x.

So the first few terms of this sequence are 1 2 4 7 9 12 18…

Given two integers A and B, find out the number of integers in the above sequence that lies within the range [A, B].

Input

The first line of input is an integer T (T < 100000), that indicates the number of test cases. Each case contains two integers, A followed by B (1 A B 1000000).

Output

For each case, output the case number first followed by the required result.

 

Sample Input

Sample Output

3

1 18

1 100

3000 4000

Case 1: 7

Case 2: 20

Case 3: 87


Problemsetter: Sohel Hafiz, Special Thanks: Shamim Hafiz

 #include <stdio.h>
#include <math.h>
int p[5200], pt = 0;
int N[100000];
void sieve() {
    int i, j, mark[50000] = {};
    for(i = 2; i < 50000; i++) {
        if(!mark[i]) {
            p[pt++] = i;
            for(j = i+i; j < 50000; j += i)
                mark[j] = 1;
        }
    }
}
int NOD(int n) {
    int ans = 1, cnt = 0, i;
    for(i = 0; i < pt && p[i]*p[i] <= n; i++) {
        if(n%p[i] == 0) {
            cnt = 0;
            while(n%p[i] == 0)
                cnt++, n /= p[i];
            ans *= cnt+1;
        }
    }
    if(n != 1)  ans *= 2;
    return ans;
}
int build() {
    N[0] = 1;
    int i;
    for(i = 1; ; i++) {
        N[i] = N[i-1] + NOD(N[i-1]);
        if(N[i] > 1000000)
            return i;
    }
}
int main() {
    sieve();
    int R = build(), L = 0;
    int t, cases = 0, A, B;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &A, &B);
        int l = L, r = R, m, x, y;
        while(l < r) {
            m = (l+r)>>1;
            if(N[m] < A)
                l = m+1;
            else
                r = m;
        }
        x = l;
        l = L, r = R;
        while(l < r) {
            m = (l+r)>>1;
            if(N[m] < B)
                l = m+1;
            else
                r = m;
        }
        y = l + (N[l] == B);
        printf("Case %d: %d\n", ++cases, y-x);
    }
    return 0;
}



台長: Morris
人氣(1,369) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][TSP] 10944 - Nuts for nuts..
此分類上一篇:[UVA][幾何] 11345 - Rectangles

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