"CQOI2015" the number of election

"CQOI2015" the number of election

Title Description

We know, from the interval [L, H] (L and H are integers) N ​​integers selected, a total of (H-L + 1) ^ N kinds of programs. Z small law very curious greatest common divisor of the number of such elected, he decided for each program selected N integers are seeking a greatest common divisor for further study. However, he soon found too much work, so to you for help. Your mission is very simple, small z an integer K will tell you, you just need to answer him the greatest common divisor is the number of the selected program K. Due to a large number of programs, you only need to export the remainder was divided by 1,000,000,007 to.

Input and output formats

Input formats:

Input line, a positive integer including four spaces separated, followed by N, K, L and H.

Output formats:

Outputs an integer number of programs for the request.

Sample input and output

Input Sample # 1: Copy
2 2 2 4
Output Sample # 1: Copy
3

Explanation

Sample interpretation

All possible options: (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 2), (4 , 3), (4, 4)

Wherein the greatest common divisor is equal to 2, only three groups: (2, 2), (2, 4), (4, 2)

To 100% of the data, 1 <= N, K <= 10 ^ 9,1 <= L <= H <= 10 ^ 9, HL <= 10 ^ 5

Du teach sieve problem solution

First, the gcd be converted to the classical K of: \ (L \) becomes \ (\ lceil \ K FRAC {L} {} \ rceil \) , the \ (R & lt \) becomes \ (\ lfloor \ frac { } {K} R & lt \ rfloor \) .
This way easy to obtain, is now required in \ ([L, R] \ ) between the number of selected \ (N \) times to the selected number of the greatest common divisor \ (1 \) number scheme.
\ [Ans = \ sum_ {i_ {1 \ sim N} = L} ^ R [\ gcd (i_ {1 \ sim N}) = 1] \\ \ sum_ {i_ {1 \ sim N} = L} ^ R \ sum_ {d | i_ { 1 \ sim N}} \ mu (d) \\ = \ sum_ {d = 1} ^ R \ mu (d) (\ lfloor \ frac Rd \ rfloor- \ lfloor \ frac { L-1} d \ rfloor)
^ N \] then added Du teach sieve to block divisible. Time complexity \ (O ((\ frac RK ) ^ \ frac 23+ \ sqrt {R} \ log N) \)

#include<bits/stdc++.h>
#define il inline
#define co const
template<class T>T read(){
    T data=0,w=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(T&x) {return x=read<T>();}
typedef long long LL;

co int N=1e6+1;
int pri[N],tot,mu[N];
void init(){
    pri[1]=mu[1]=1;
    for(int i=2;i<N;++i){
        if(!pri[i]) pri[++tot]=i,mu[i]=-1;
        for(int j=1;j<=tot&&i*pri[j]<N;++j){
            pri[i*pri[j]]=1;
            if(i%pri[j]==0){
                mu[i*pri[j]]=0;
                break;
            }
            mu[i*pri[j]]=-mu[i];
        }
    }
    for(int i=2;i<N;++i) mu[i]+=mu[i-1];
}
std::map<int,int> smu;
int Mu(int n){
    if(n<N) return mu[n];
    if(smu.count(n)) return smu[n];
    int ans=1;
    for(int l=2,r;l<=n;l=r+1){
        r=n/(n/l);
        ans-=(r-l+1)*Mu(n/l);
    }
    return smu[n]=ans;
}
co int mod=1e9+7;
il int add(int a,int b){
    return (a+=b)>=mod?a-mod:a;
}
il int mul(int a,int b){
    return (LL)a*b%mod;
}
int fpow(int a,int b){
    int ans=1;
    for(;b;b>>=1,a=mul(a,a))
        if(b&1) ans=mul(ans,a);
    return ans;
}
int main(){
    init();
    int n=read<int>(),k=read<int>();
    int L=(read<int>()-1)/k+1,R=read<int>()/k;
    int ans=0;
    for(int l=1,r;l<=R;l=r+1){
        if((L-1)/l) r=std::min((L-1)/((L-1)/l),R/(R/l)); // edit 1:/0
        else r=R/(R/l);
        ans=add(ans,add(mod,mul(Mu(r)-Mu(l-1),fpow(R/l-(L-1)/l,n))));
    }
    printf("%d\n",ans);
    return 0;
}

Note: When the block is divisible, since the upper limit is R, the L-1 may be the case that lump divide by zero occurs, needs Laid sentence.

xyz32768 inclusion and exclusion of the solution to a problem

Is now required in \ ([L, H] \ ) between the number of selected \ (N \) times to the selected number of the greatest common divisor \ (1 \) number scheme.

Now, with \ (f [i] \) represents the greatest common divisor of the number of selected \ (I \) and the number of the selected program number of the same failure. At this time, determined to \ ([L, H] \ ) between \ (I \) the number of multiples of \ (X \) , temporarily Order \ (F [I] = ^ Nx of X \) .

But this time the resulting \ (f [i] \) actually contains divisor \ (I \) program number, the greatest common divisor is not \ (I \) program number. It can be found, in this case \ (f [i] \) with the greatest common divisor of \ (i, 2i, 3i, ... \) the number of programs. This time using the inclusion-exclusion principle: Suppose already know \ (f [2i], f [3i], ... \) is the end result, then put \ (f [i] \) subtracting \ (f [ 2i], F [3i], ... \) , can be obtained \ (f [i] \) of the final result. Push backwards again.

Special case: \ (L = 1 \) can be selected when all the numbers are \ (1 \) . So \ (L = 1 \) when the answer to add \ (1 \) .

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}
const int N = 1e5 + 5, PYZ = 1e9 + 7;
int n, K, L, H, f[N];
int qpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = 1ll * res * a % PYZ;
        a = 1ll * a * a % PYZ;
        b >>= 1;
    }
    return res;
}
int main() {
    int i, j; n = read(); K = read(); L = read(); H = read();
    if (L % K) L = L / K + 1; else L /= K; H /= K;
    if (L > H) return puts("0"), 0;
    for (i = 1; i <= H - L; i++) {
        int l = L, r = H;
        if (l % i) l = l / i + 1; else l /= i; r /= i;
        if (l > r) continue;
        f[i] = (qpow(r - l + 1, n) - (r - l + 1) + PYZ) % PYZ;
    }
    for (i = H - L; i; i--) for (j = (i << 1); j <= H - L; j += i)
        f[i] = (f[i] - f[j] + PYZ) % PYZ;
    if (L == 1) (f[1] += 1) %= PYZ; cout << f[1] << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/11110984.html
Recommended