☆【容斥原理】【SCOI2010】幸运数字

来源:互联网 发布:windows网络编程百度云 编辑:程序博客网 时间:2024/05/17 09:07
【题目描述】在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是太少了,比如在[1,100]的区间内就只有6个(6,8,66,68,86,88),于是他又定义了一种“近似幸运号码”。lxhgww规定,凡是“幸运号码”的倍数都是“近似幸运号码”,当然,任何的“幸运号码”也都是“近似幸运号码”,比如12,16,666都是“近似幸运号码”。现在lxhgww想知道在一段闭区间[a, b]内,“近似幸运号码”的个数。【输入】输入数据是一行,包括2个数字a和b【输出】输出数据是一行,包括1个数字,表示在闭区间[a, b]内“近似幸运号码”的个数【样例输入1】1 10【样例输出1】2【样例输入2】1234 4321【样例输出2】809【数据范围】   对于30%的数据,保证1<=a<=b<=1000000   对于100%的数据,保证1<=a<=b<=10000000000
这是一道使用容斥原理并剪枝优化的题。

首先找出所有的幸运数字的基数(即只含数字6和8的数),然后根据容斥原理的基本方法零壹枚举,具体细节如程序注释所示。
Accode:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <bitset>using std::cin;using std::cout;typedef long long int64;const char fi[] = "luckynumber.in";const char fo[] = "luckynumber.out";const int maxN = 2050;int64 tmp[maxN];int64 Enum[maxN];int n, tot;int64 a, b, m;void init_file(){freopen(fi, "r", stdin);freopen(fo, "w", stdout);std::ios::sync_with_stdio(false);return;}void readdata(){    cin >> a >> b; --a;return;}void mknum(int64 p){    if (p > b) return;    if (p > 0) tmp[++tot] = p;    mknum(p * 10LL + 6LL);    mknum(p * 10LL + 8LL);    return;}int cmp(const void *a, const void *b){    if (*(int64 *)b > *(int64 *)a) return 1;    if (*(int64 *)b < *(int64 *)a) return -1;    return 0;} //对int64型的数排序只能这样。void modify(){    qsort(tmp + 1, tot, sizeof(tmp[0]), cmp);    for (int i = 1; i < tot + 1; ++i)    {        bool flag = true;        for (int j = tot; j > i; --j)        if (tmp[i] % tmp[j] == 0)        {            flag = false;            break;        }        if (flag) Enum[++n] = tmp[i];    }//将以上生成的基数中能被其它较小的基数整除的筛掉,//并从大到小排序,尽量较早地剪枝。    return;}inline int64 gcd(int64 a, int64 b){    int64 tmp;    while (b)    {        tmp = b;        b = a % b;        a = tmp;    }    return a;}int64 Dfs(int i, int64 num){    if (i > n) return b / num - a / num;    int64 ans = Dfs(i + 1, num);//若当前这个待枚举的数未被选,//则不改变枚举总个数的奇偶性,//就不翻转Dfs()前的符号。    int64 g = gcd(num, Enum[i]);    if (num / g <= b / Enum[i])        ans -= Dfs(i + 1, num / g * Enum[i]);//若Lcm(num, Enum[i]) <= b,则当前待枚举的数//可以被选,枚举时由于总个数增加了1,改变了//奇偶性,所以在Dfs()之前添加负号,就会形成//一层一层嵌套的负号的式子。    return ans;}void work(){    mknum(6LL); mknum(8LL);    modify();    cout << b - a - Dfs(1, 1) << std::endl;//输出最后结果的时候,记得Dfs()模块求出来的//是补集,需要对其取反。return;}int main(){init_file();readdata();work();return 0;}
第二次做:
#include <cstdio>typedef long long int64;const int64 Enum[] = {8888888866LL,8888888686LL,8888888666LL,8888886886LL,8888886868LL,8888886866LL,8888886686LL,8888886668LL,8888868886LL,8888868868LL,8888868866LL,8888868686LL,8888866886LL,8888866868LL,8888866666LL,8888688868LL,8888688866LL,8888688686LL,8888688668LL,8888686886LL,8888686868LL,8888686666LL,8888668886LL,8888668868LL,8888668666LL,8888666866LL,8888666686LL,8888666668LL,8888666666LL,8886888886LL,8886888866LL,8886888686LL,8886886886LL,8886886868LL,8886886666LL,8886868886LL,8886868868LL,8886868666LL,8886866866LL,8886866686LL,8886866668LL,8886866666LL,8886688886LL,8886688666LL,8886686866LL,8886686686LL,8886686668LL,8886686666LL,8886668866LL,8886668686LL,8886668668LL,8886668666LL,8886666886LL,8886666868LL,8886666866LL,8886666686LL,8886666668LL,8868888886LL,8868888868LL,8868888866LL,8868888686LL,8868888668LL,8868886886LL,8868886868LL,8868886666LL,8868868886LL,8868868868LL,8868868666LL,8868866866LL,8868866686LL,8868866668LL,8868866666LL,8868688886LL,8868688868LL,8868688666LL,8868686866LL,8868686686LL,8868686668LL,8868686666LL,8868668866LL,8868668686LL,8868668668LL,8868666886LL,8868666868LL,8868666866LL,8868666686LL,8868666668LL,8866888886LL,8866888868LL,8866888666LL,8866886866LL,8866886686LL,8866886668LL,8866886666LL,8866868866LL,8866868686LL,8866868668LL,8866868666LL,8866866886LL,8866866866LL,8866866668LL,8866688866LL,8866688686LL,8866688668LL,8866686886LL,8866686868LL,8866686866LL,8866686686LL,8866668868LL,8866668866LL,8866668686LL,8866668668LL,8866666886LL,8866666868LL,8866666666LL,8688888886LL,8688888868LL,8688888866LL,8688888686LL,8688888668LL,8688886886LL,8688886868LL,8688886666LL,8688868886LL,8688868868LL,8688868666LL,8688866866LL,8688866686LL,8688866666LL,8688688886LL,8688688868LL,8688688666LL,8688686866LL,8688686686LL,8688686668LL,8688686666LL,8688668866LL,8688668686LL,8688668668LL,8688666886LL,8688666868LL,8688666866LL,8688666686LL,8688666668LL,8686888886LL,8686888868LL,8686888666LL,8686886866LL,8686886686LL,8686886668LL,8686886666LL,8686868866LL,8686868668LL,8686868666LL,8686866886LL,8686866868LL,8686866866LL,8686866686LL,8686866668LL,8686688866LL,8686688686LL,8686688668LL,8686688666LL,8686686886LL,8686686868LL,8686686686LL,8686686668LL,8686668886LL,8686668868LL,8686668866LL,8686668686LL,8686668668LL,8686666886LL,8686666666LL,8668888886LL,8668888868LL,8668886866LL,8668886686LL,8668886668LL,8668886666LL,8668868866LL,8668868686LL,8668868668LL,8668868666LL,8668866886LL,8668866868LL,8668866866LL,8668866686LL,8668866668LL,8668688866LL,8668688686LL,8668688668LL,8668688666LL,8668686886LL,8668686868LL,8668686866LL,8668686668LL,8668668886LL,8668668868LL,8668668866LL,8668668686LL,8668668668LL,8668666886LL,8668666868LL,8668666666LL,8666888866LL,8666888686LL,8666888668LL,8666888666LL,8666886886LL,8666886868LL,8666886866LL,8666886686LL,8666868886LL,8666868868LL,8666868866LL,8666868686LL,8666866886LL,8666866868LL,8666866666LL,8666688886LL,8666688868LL,8666688866LL,8666688686LL,8666688668LL,8666686886LL,8666686868LL,8666668886LL,8666668868LL,8666668666LL,8666666866LL,8666666686LL,8666666668LL,8666666666LL,6888888886LL,6888888868LL,6888888866LL,6888888686LL,6888888668LL,6888886886LL,6888886868LL,6888886666LL,6888868886LL,6888868868LL,6888868666LL,6888866866LL,6888866668LL,6888866666LL,6888688886LL,6888688868LL,6888686866LL,6888686686LL,6888686668LL,6888686666LL,6888668866LL,6888668686LL,6888668666LL,6888666886LL,6888666868LL,6888666866LL,6888666686LL,6888666668LL,6886888886LL,6886888868LL,6886888666LL,6886886866LL,6886886686LL,6886886668LL,6886886666LL,6886868866LL,6886868686LL,6886868668LL,6886868666LL,6886866886LL,6886866868LL,6886866866LL,6886866686LL,6886688866LL,6886688686LL,6886688668LL,6886688666LL,6886686886LL,6886686868LL,6886686866LL,6886686686LL,6886686668LL,6886668886LL,6886668868LL,6886668686LL,6886668668LL,6886666886LL,6886666868LL,6886666666LL,6868888868LL,6868888666LL,6868886866LL,6868886686LL,6868886668LL,6868886666LL,6868868866LL,6868868686LL,6868868668LL,6868868666LL,6868866886LL,6868866868LL,6868866866LL,6868866686LL,6868866668LL,6868688866LL,6868688686LL,6868688668LL,6868688666LL,6868686886LL,6868686866LL,6868686686LL,6868686668LL,6868668886LL,6868668868LL,6868668866LL,6868668668LL,6868666886LL,6868666868LL,6868666666LL,6866888866LL,6866888686LL,6866888668LL,6866888666LL,6866886886LL,6866886868LL,6866886866LL,6866886686LL,6866886668LL,6866868886LL,6866868868LL,6866868866LL,6866868686LL,6866866886LL,6866866868LL,6866866666LL,6866688886LL,6866688868LL,6866688866LL,6866688686LL,6866688668LL,6866686886LL,6866686868LL,6866686666LL,6866668886LL,6866668868LL,6866666866LL,6866666686LL,6866666668LL,6866666666LL,6688888886LL,6688888868LL,6688888666LL,6688886866LL,6688886686LL,6688886668LL,6688886666LL,6688868866LL,6688868686LL,6688868668LL,6688868666LL,6688866886LL,6688866868LL,6688866866LL,6688866686LL,6688866668LL,6688688866LL,6688688686LL,6688688668LL,6688688666LL,6688686886LL,6688686868LL,6688686866LL,6688686686LL,6688686668LL,6688668886LL,6688668868LL,6688668866LL,6688668686LL,6688668668LL,6688666868LL,6688666666LL,6686888866LL,6686888686LL,6686888668LL,6686888666LL,6686886886LL,6686886868LL,6686886866LL,6686886686LL,6686886668LL,6686868886LL,6686868868LL,6686868866LL,6686868686LL,6686868668LL,6686866886LL,6686866666LL,6686688886LL,6686688868LL,6686688866LL,6686688686LL,6686688668LL,6686686886LL,6686686868LL,6686686666LL,6686668886LL,6686668868LL,6686668666LL,6686666686LL,6686666666LL,6668888866LL,6668888686LL,6668888668LL,6668888666LL,6668886886LL,6668886868LL,6668886866LL,6668886686LL,6668886668LL,6668868886LL,6668868866LL,6668868686LL,6668868668LL,6668866886LL,6668866868LL,6668866666LL,6668688886LL,6668688868LL,6668688866LL,6668688686LL,6668686886LL,6668686868LL,6668686666LL,6668668886LL,6668668868LL,6668668666LL,6668666866LL,6668666668LL,6668666666LL,6666888886LL,6666888868LL,6666888866LL,6666888686LL,6666888668LL,6666886886LL,6666886868LL,6666886666LL,6666868886LL,6666868868LL,6666868666LL,6666866866LL,6666866686LL,6666866666LL,6666688886LL,6666688666LL,6666686866LL,6666686686LL,6666686668LL,6666686666LL,6666668866LL,6666668686LL,6666668668LL,6666668666LL,6666666886LL,6666666868LL,6666666866LL,6666666686LL,6666666668LL,888888886LL,888888868LL,888888866LL,888888686LL,888888668LL,888886886LL,888886868LL,888886666LL,888868886LL,888868666LL,888866866LL,888866686LL,888866668LL,888866666LL,888688886LL,888688868LL,888688666LL,888686866LL,888686686LL,888686668LL,888686666LL,888668866LL,888668686LL,888668668LL,888668666LL,888666886LL,888666868LL,888666866LL,888666686LL,888666668LL,886888886LL,886888868LL,886888666LL,886886866LL,886886686LL,886886666LL,886868866LL,886868686LL,886868668LL,886868666LL,886866886LL,886866868LL,886866866LL,886866686LL,886866668LL,886688866LL,886688686LL,886688668LL,886688666LL,886686886LL,886686868LL,886686866LL,886686686LL,886686668LL,886668886LL,886668868LL,886668866LL,886668668LL,886666886LL,886666868LL,886666666LL,868888886LL,868888868LL,868888666LL,868886866LL,868886686LL,868886668LL,868886666LL,868868866LL,868868686LL,868868668LL,868868666LL,868866886LL,868866868LL,868866866LL,868688866LL,868688686LL,868688668LL,868686886LL,868686868LL,868686866LL,868686686LL,868686668LL,868668868LL,868668866LL,868668686LL,868668668LL,868666886LL,868666868LL,868666666LL,866888866LL,866888668LL,866888666LL,866886886LL,866886868LL,866886866LL,866886686LL,866886668LL,866868886LL,866868868LL,866868866LL,866868686LL,866868668LL,866866886LL,866866868LL,866866666LL,866688886LL,866688868LL,866688866LL,866688686LL,866688668LL,866686886LL,866686868LL,866686666LL,866668886LL,866668868LL,866668666LL,866666866LL,866666686LL,866666668LL,866666666LL,688888886LL,688888868LL,688888666LL,688886866LL,688886686LL,688886668LL,688886666LL,688868866LL,688868668LL,688868666LL,688866886LL,688866868LL,688866866LL,688866686LL,688866668LL,688688866LL,688688668LL,688688666LL,688686886LL,688686868LL,688686866LL,688686686LL,688686668LL,688668886LL,688668868LL,688668866LL,688668686LL,688668668LL,688666886LL,688666868LL,686888866LL,686888686LL,686888668LL,686888666LL,686886886LL,686886868LL,686886866LL,686886686LL,686886668LL,686868886LL,686868868LL,686868866LL,686868686LL,686868668LL,686866886LL,686866868LL,686866666LL,686688886LL,686688868LL,686688866LL,686688686LL,686688668LL,686686886LL,686686868LL,686686666LL,686668886LL,686668868LL,686668666LL,686666866LL,686666686LL,686666668LL,686666666LL,668888866LL,668888686LL,668888668LL,668888666LL,668886886LL,668886868LL,668886866LL,668886686LL,668886668LL,668868886LL,668868868LL,668868686LL,668868668LL,668866886LL,668866868LL,668866666LL,668688886LL,668688868LL,668688866LL,668688686LL,668688668LL,668686886LL,668686868LL,668686666LL,668668886LL,668668868LL,668668666LL,668666866LL,668666686LL,668666668LL,668666666LL,666888886LL,666888868LL,666888866LL,666888686LL,666888668LL,666886886LL,666886868LL,666886666LL,666868886LL,666868868LL,666868666LL,666866866LL,666866686LL,666866668LL,666866666LL,666688886LL,666688868LL,666688666LL,666686866LL,666686686LL,666686668LL,666686666LL,666668866LL,666668686LL,666668666LL,666666886LL,666666868LL,666666866LL,666666686LL,88888886LL,88888868LL,88888666LL,88886866LL,88886686LL,88886668LL,88886666LL,88868866LL,88868686LL,88868668LL,88868666LL,88866886LL,88866868LL,88866866LL,88866668LL,88688866LL,88688686LL,88688668LL,88688666LL,88686886LL,88686868LL,88686866LL,88686686LL,88686668LL,88668886LL,88668868LL,88668686LL,88666886LL,88666868LL,88666666LL,86888866LL,86888686LL,86888668LL,86888666LL,86886886LL,86886868LL,86886866LL,86886686LL,86886668LL,86868886LL,86868868LL,86868668LL,86866886LL,86866868LL,86866666LL,86688886LL,86688868LL,86688866LL,86688686LL,86686886LL,86686868LL,86686666LL,86668886LL,86668868LL,86666866LL,86666686LL,86666668LL,86666666LL,68888866LL,68888686LL,68888668LL,68886886LL,68886868LL,68886866LL,68886686LL,68886668LL,68868868LL,68868866LL,68868686LL,68868668LL,68866868LL,68866666LL,68688886LL,68688868LL,68688866LL,68688686LL,68688668LL,68686886LL,68686666LL,68668886LL,68668868LL,68668666LL,68666686LL,68666668LL,68666666LL,66888886LL,66888868LL,66888866LL,66888686LL,66888668LL,66886886LL,66886868LL,66886666LL,66868886LL,66868868LL,66868666LL,66866866LL,66866668LL,66866666LL,66688886LL,66688868LL,66688666LL,66686866LL,66686686LL,66686666LL,66668866LL,66668686LL,66668668LL,66668666LL,66666886LL,66666868LL,66666866LL,66666686LL,66666668LL,8888866LL,8888686LL,8888668LL,8888666LL,8886886LL,8886868LL,8886866LL,8886686LL,8886668LL,8868886LL,8868868LL,8868866LL,8868686LL,8868668LL,8866886LL,8866868LL,8866666LL,8688886LL,8688868LL,8688866LL,8688686LL,8688668LL,8686886LL,8686868LL,8686666LL,8668868LL,8668666LL,8666866LL,8666686LL,8666666LL,6888886LL,6888868LL,6888866LL,6888668LL,6886886LL,6886868LL,6886666LL,6868886LL,6868868LL,6868666LL,6866866LL,6866686LL,6866668LL,6866666LL,6688886LL,6688868LL,6688666LL,6686866LL,6686686LL,6686668LL,6686666LL,6668866LL,6668686LL,6668668LL,6668666LL,6666886LL,6666868LL,6666866LL,6666686LL,6666668LL,888886LL,888868LL,888866LL,888686LL,888668LL,886868LL,886666LL,868886LL,868666LL,866686LL,866668LL,866666LL,688886LL,688868LL,688666LL,686866LL,686668LL,686666LL,668866LL,668686LL,668666LL,666886LL,666868LL,666866LL,666686LL,666668LL,88886LL,88868LL,86866LL,86686LL,86668LL,86666LL,68866LL,68686LL,68668LL,68666LL,66886LL,66868LL,66866LL,66686LL,66668LL,8866LL,8668LL,8666LL,6886LL,6866LL,6686LL,6668LL,886LL,868LL,866LL,686LL,668LL,86LL,68LL,8LL,6LL,0};int64 a, b;inline int64 gcd(int64 a, int64 b){    while (b)    {int64 tmp = a % b; a = b; b = tmp;}    return a;}int64 Dfs(int i, int64 Last){    if (!Enum[i]) return b / Last - a / Last;    int64 tmp = Dfs(i + 1, Last);    int64 g = gcd(Last, Enum[i]);    if (Last / g <= b / Enum[i])        tmp -= Dfs(i + 1, Last / g * Enum[i]);    return tmp;}int main(){    freopen("luckynumber.in", "r", stdin);    freopen("luckynumber.out", "w", stdout);    scanf("%I64d%I64d", &a, &b); --a;    printf("%I64d\n", b - a - Dfs(0, 1));    return 0;}

原创粉丝点击