Bzoj 1853 "Scoi2010" Lucky numbers

Source: Internet
Author: User

Description in China, many people think of 6 and 8 as lucky numbers! LXHGWW also thought, so he defined his "lucky number" is a decimal representation of only the numbers 6 and 8 of those numbers, such as 68,666,888 are "lucky number"! But this "lucky number" is always too small, for example, in the [1,100] range of only 6 (6,8,66,68,86,88), so he defined a "approximate lucky number." LXHGWW rules that the "lucky number" multiples are "approximate lucky numbers", of course, any "lucky number" is also "approximate lucky number", such as 12, 16,666 are "approximate lucky number." Now LXHGWW want to know the number of "approximate lucky numbers" within a closed interval [a, b]. Input data is a row, including 2 numbers A and boutput output data is a row, including 1 numbers, indicating the number of "approximate lucky numbers" within the closed interval [a, b] hint "data range"
For $30\%$ data, ensure that \leqslant a \leqslant b \leqslant1000000$
For $100\%$ data, guaranteed $ \leqslant a \leqslant b \leqslant 10000000000$  the question at first I thought I'd need some magic mathematical derivation, or some wonderful mathematical formula,  Then read the problem after the discovery is a search question ... A very obvious fact is that the lucky number is not too much.  By playing the watch, you will find that the lucky number within $10^{10}$ is only $2000$ a little more ... At this time, a very obvious idea is to take these numbers, that is, add each number of times, minus two number of times the number of numbers, plus three, ...  And so on This complexity is obviously wrong, theoretically up to $o (2^x) $, where $x$ is a lucky number.  However, because multiple numbers cannot exceed the right edge, you can reduce a machete, the actual complexity of the low do not know how much. But that's not enough. We can also deal with the lucky number, which is a multiple of the number of other lucky number to be removed.  This will reduce the number of items that need to be considered by about half. Finally, there is a small optimization, that is, the last need to deal with the number of lucky in order from the big to the small row.  This allows the product to become larger as soon as possible, and to reduce many unnecessary calculations.  By adding the above optimization, it is almost possible to $ac$. Paste the following code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define FILE (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout) #define MAXN 10010using namespace std; typedef long LONG Llg;int LA,LB,CI;LLG now,ans,mi[15],l,r;llg a[maxn],b[maxn];void Search (int d) {if (now>r) return; LLG Xx=now;now=xx+6*mi[d]; Search (d+1); now=xx+8*mi[d]; Search (d+1); if (xx) a[++la]=xx; Now=xx;} Llg gcd (llg a,llg b) {Llg r=a%b;while (r) A=b,b=r,r=a%b;return b;} void Dfs (int j) {if (j==lb+1) {if (!CI) return;if (ci&1) ans+=r/now-(L-1)/now;else ans-=r/now-(L-1)/now;return;} DFS (J+1); LLG Xx=now; CI++;NOW=XX/GCD (Xx,b[j]), if (double) now*b[j]<=r) {now*=b[j];if (now<=r) DFS (j+1);} ci--; Now=xx;} int main () {File ("a"), mi[0]=1;for (int i=1;i<=10;i++) mi[i]=mi[i-1]*10;scanf ("%lld%lld", &l,&r); Search (0) ; Sort (a+1,a+la+1), for (int i=1;i<=la;i++) {b[++lb]=a[i];for (int. j=1;j<lb;j++) if (a[i]%b[j]==0) {lb--; break;}} for (int i=1;i<=lb/2;i++) swap (b[i],b[lb-I+1]); now=1; DFS (1);p rintf ("%lld", ans); return 0;}

Bzoj 1853 "Scoi2010" Lucky 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.