"Recursion" Bzoj 3930: [CQOI2015] Select number

Source: Internet
Author: User
Tags greatest common divisor

Description

We know that n integers are selected from the interval [l,h] (L and H as integers), with a total of (h-l+1) ^n schemes. Little Z was curious about the law of the greatest common divisor of the chosen number, and he decided to seek a greatest common divisor for the n integers chosen for each scheme for further study. However, he quickly found that the workload was too large and asked you for help. Your task is very simple, small z will tell you an integer k, you need to answer how many of the options he greatest common divisor just for K. Because of the large number of scenarios, you only need to output the remainder of the 1000000007.

Input

Enter a line that contains 4 spaces separated by a positive integer, n,k,l and h in turn.

Output

Outputs an integer that is the number of the scheme you are seeking.

There are two ways of doing this problem--

1. Recursion

We first select n elements between an interval [l,r] and their gcd for K*i is f[i].

Obviously, [l,r] is divisible by k*i (r-l+1) ^n (r=r/(i*k), l=l/(I*k)). However, there are some options for this (l,l,l,l,l,l,... L), there are (r-l+1) species, while there are greatest common divisor are multiples of k*i, we also want to subtract.

Get f[i]= (r-l+1) ^n-(r-l+1)-f[k*i*a] (a>=2 && k*i*a<=l-r+1).

Output F[1].

But there are special circumstances. is k in [L,r], so this time f[1]++ can.

2.mobius Inversion

The formula is pretty easy.

Mobius Formula derivation: http://lzy-foenix.gitcafe.io/2015/04/09/BZOJ-3930-CQOI2015-%E9%80%89%E6%95%B0/

On the derivation of the threshold and μ: http://www.cnblogs.com/Asm-Definer/p/4434601.html

POPOQQQ combination of both: http://blog.csdn.net/popoqqq/article/details/44917831 (picture quality moving--)

My Code

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 6 #defineMoD 10000000077 8 #defineMAXN 1000009 Ten using namespacestd; One  A Long Longf[maxn+1]; -  - Long LongQvodLong LongXLong Longk) the { -     Long Longans=1; -      while(k!=0) -     { +         if(k&1) ans=ans*x%MoD; -x=x*x%MoD; +k>>=1; A     } at     returnans; - } -  - intMain () - { -     intA,b,k,n; inscanf"%d%d%d%d",&n,&k,&a,&b); -     intl=a/k,r=b/K; to     if(a%k) l++; +      for(inti=maxn;i>=1; i--) -     { the         intl=l/i,r=r/i; *         if(l%i) l++; $         if(l<=R)Panax Notoginseng         { -F[i]=qvod (r-l+1, n); theF[i]= (f[i]-(r-l+1) +mod)%MoD; +              for(intj=i*2; j<=maxn;j+=i) f[i]= (f[i]-f[j]+mod)%MoD; A         } the     } +     if(l==1) f[1]++; -printf"%lld", (f[1]+MOD)%MoD); $     return 0; $}
View Code

Ignoring the strange fast power

"Recursion" Bzoj 3930: [CQOI2015] Select number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.