Codeforces Round #392 (Div. 2) F. Geometrical Progression

分類:技術 時間:2017-01-20

原題地址:http://codeforces.com/contest/758/problem/F

F. Geometrical Progression

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

For given n, l and r find the number of distinct geometrical progression, each of which contains n distinct integers not less than l and not greater than r. In other words, for each progression the following must hold: l?≤?ai?≤?r and ai?≠?aj , where a1,?a2,?...,?an is the geometrical progression, 1?≤?i,?j?≤?n and i?≠?j.

Geometrical progression is a sequence of numbers a1,?a2,?...,?an where each term after first is found by multiplying the previous one by a fixed non-zero number d called the common ratio. Note that in our task d may be non-integer. For example in progression 4,?6,?9, common ratio is .

Two progressions a1,?a2,?...,?an and b1,?b2,?...,?bn are considered different, if there is such i (1?≤?i?≤?n) that ai?≠?bi.

Input

The first and the only line cotains three integers n, l and r (1?≤?n?≤?107,?1?≤?l?≤?r?≤?107).

Output

Print the integer K — is the answer to the problem.

Examples

Input

1 1 10

Output

10

Input

2 6 9

Output

12

Input

3 1 10

Output

8

Input

3 3 10

Output

2

Note

These are possible progressions for the first test of examples:

  • 1;
  • 2;
  • 3;
  • 4;
  • 5;
  • 6;
  • 7;
  • 8;
  • 9;
  • 10.

These are possible progressions for the second test of examples:

  • 6,?7;
  • 6,?8;
  • 6,?9;
  • 7,?6;
  • 7,?8;
  • 7,?9;
  • 8,?6;
  • 8,?7;
  • 8,?9;
  • 9,?6;
  • 9,?7;
  • 9,?8.

These are possible progressions for the third test of examples:

  • 1,?2,?4;
  • 1,?3,?9;
  • 2,?4,?8;
  • 4,?2,?1;
  • 4,?6,?9;
  • 8,?4,?2;
  • 9,?3,?1;
  • 9,?6,?4.

These are possible progressions for the fourth test of examples:

  • 4,?6,?9;
  • 9,?6,?4.

題意:給定 n, l and r ,求項數為n, 公比不為1,且數列每一項都屬於[l,r]範圍的不同的 等比數列 的個數。

題解:其實是先縮小範圍然後直接枚舉。

考慮數據範圍1?≤?n?≤?107,?1?≤?l?≤?r?≤?10

設等比數列公比為d, d表示為 q/p,其中q或p為不同時等於1,且互質的正整數。

遞增和遞減數列的情況是成對出現的,即p和q互換。

所以不妨只考慮遞增數列的情況,即公比d表示為q/p,其中pq互質,p為任意正整數,q>p,q為大於等於2的正整數。

則數列末項整除於qn-1 ,其中q>=2,2^24>10^7, 故n>=24時無解。

n=1時為結果為r-l+1, n=2時結果為(r-l+1)*(r-l),n>24時0.

n>=3&&n<24時,可以通過枚舉出p和q的情況求解。

n>=3, 由於數列末項整除於qn-1 ,則qn-1 ≤?107,即枚舉 p,q的上界是(1071/(n-1),當n=3時,這個值為3162,可以通過暴力枚舉實現。

枚舉p,q,

每找到一對(p,q)且gcd(p,q)==1

考慮數列末項  an= a1*qn-1/pn-1  ,

要滿足 a1>=l, an<=r 的範圍條件,若 l*qn-1/pn-1 >r 則不滿足題意,continue;

若 l*qn-1/pn-1 <=r 則有滿足[l,r]範圍的等比數列

現在求[l,r]範圍,公比為q/p,項數為n的等比數列的個數。

數列各項為 a1, a1*q/p ……a1*qn-1/qn-1qn-1pn-1  /pn-1 /pn-1 pn-1q/pq/pq/pn-1 ,等比數列的個數即為a1可能的值。

末項為moa1*qn-1/ pn-1  所以a1必整除於pn-1 ,即a1可能的值為 [l,r*pn-1/qn-1]範圍內可被 pn-1整除的, 即 (r*pn-1/qn-1)/pn-1-l/pn-1

#include <bits/stdc++.h>
#define LL long long
using namespace std;

LL gcd(LL a, LL b){
    if(b==0) return a;
    else return gcd(b,a%b);
}

LL QuickPow(LL a, LL n){
    LL ret=1;
    while(n){
        if(n&1) ret*=a;
        a*=a;
        n>>=1;
    }
    return ret;
}

LL l,r,n;
LL ans;

int main()
{
    cin>>n>>l>>r;
    if(n>24){
        cout<<0;return 0;
    }
    if(n==1){
        cout<<r-l+1;return 0;
    }
    if(n==2){
        cout<<(r-l+1)*(r-l);return 0;
    }

    //n>=3&&n<24的情況
    LL upperlimit,pn,qn;
    //p,q的枚舉上界
    upperlimit=pow(2,double(log2(1e7+50)/(n-1))); //註意精度
    for(LL p=1;p<=upperlimit;p++)
        for(LL q=p+1;q<=upperlimit;q++)
            if(gcd(p,q)==1)
            {
                qn=QuickPow(q,n-1);
                pn=QuickPow(p,n-1);
                if(l*qn/pn>r) continue;

                //a1可能的值 :[l,r*pn/qn]範圍內可被 pn整除的正整數,
                ans+=(r*pn/qn)/pn-(l-1)/pn;
            }
       //遞增數列遞減數列成對出現,只考慮了遞增數列
        cout<<ans*2;
        return 0;
}

a1*qn-1/qn-1qn-1pn-1  /pn-1 /pn-1 pn-1q/pq/pq/pn-1 qn-1/qn-1qn-1pn-1  /pn-1 /pn-1 pn-1q/pq/pq/pn的


Tags: Codeforces math

文章來源:


ads
ads

相關文章
ads

相關文章

ad