SCU3502 The Almost Lucky Number
Description
A lucky number is a number whose decimal representation contains only the digits \(4\) and \(7\). An almost lucky number is a number that is divisible by a lucky number. For example, \(14\), \(36\) and \(747\) are almost lucky, but \(2\) and \(17\) are not. Note that a number can be both lucky and almost lucky at the same time (for example, \(747\)).
You are given long longs a and b. Return the number of almost lucky numbers between \(a\) and$ \(b\) ,inclusive.
Input
Multiple test cases.
Each test cases is two number \(a\),\(b\) in a line seperated by a space.
\(a\) will be between \(1\) and \(10^{10}\), inclusive.
\(b\) will be between \(a\) and \(10^{10}\), inclusive.
Output
For each test case, output the answer in a single line.
Sample Input
1 10
14 14
1 100
1234 4321
Sample Output
3
1
39
1178
首先不难想到\([1,10^{10}]\)内的lucky number不多,我们可以枚举出来,且可以去除包含关系(即若\(a \mid b\),\(b\)就没有存在的必要了)。这样算下来本质上有用的lucky number个数\(N\)只是\(O(10^3)\)级别。
首先答案每次区间可减性,即\([a,b]\)的答案为\([1,b]\)的答案减去\([1,a-1]\)答案。对于区间\([1,n]\),我们显然可以用\(2^N\)的容斥原理。但是\(N\)太大,看上去\(2^N\)会TLE。但是由于LCM变化太大,当LCM比\(n\)大时候break复杂度就在期望的范围内了。但是可能需要卡卡常数(我懒得卡了)。
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
#define maxn (10010)
ll A,B,lucky[maxn]; int tot,N; bool exist[maxn];
inline ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; }
inline void dfs(ll num,int logten)
{
	if (logten > 10) return;
	if (logten) lucky[++tot] = num;
	dfs(num*10LL+4LL,logten+1); dfs(num*10LL+7LL,logten+1);
}
inline void DFS(ll range,ll &res,ll lcm,int now,int f)
{
	if (now > N) { if (lcm > 1) res += (range/lcm)*f; return; }
	DFS(range,res,lcm,now+1,f);
	ll d = gcd(lcm,lucky[now]);
	if (lcm <= (range*d+lucky[now])/lucky[now]&&lcm/d*lucky[now]<=range)
		DFS(range,res,lcm/d*lucky[now],now+1,-f);
}
inline ll calc(ll range)
{
	ll ret = 0;
	DFS(range,ret,1,1,-1);
	return ret;
}
int main()
{
	freopen("3502.in","r",stdin);
	freopen("3502.out","w",stdout);
	dfs(0,0);
	for (int i = 1;i <= tot;++i)
		for (int j = 1;j <= tot;++j)
		{
			if (i == j) continue;
			if (!(lucky[i] % lucky[j])) exist[i] = true;
		}
	for (int i = 1;i <= tot;++i) if (!exist[i]) lucky[++N] = lucky[i];
	while (scanf("%lld %lld",&A,&B) != EOF) printf("%lld\n",calc(B)-calc(A-1));
	fclose(stdin); fclose(stdout);
	return 0;
}
SCU3502 The Almost Lucky Number的更多相关文章
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
		Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ... 
- HDOJ 4937 Lucky Number
		当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ... 
- 题目1380:lucky number
		转载请注明文本链接 http://blog.csdn.net/yangnanhai93/article/details/40441709 题目链接地址:http://ac.jobdu.com/prob ... 
- HDU 3346 Lucky Number
		水题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ... 
- 九度oj 题目1380:lucky number
		题目描述: 每个人有自己的lucky number,小A也一样.不过他的lucky number定义不一样.他认为一个序列中某些数出现的次数为n的话,都是他的lucky number.但是,现在这个序 ... 
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]- Nearly Lucky Number(Codeforces Beta Round #84 (Div. 2 Only)A. Nearly)
		A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes input stand ... 
- ZOJ 3233 Lucky Number
		Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ... 
- B - Nearly Lucky Number
		Problem description Petya loves lucky numbers. We all know that lucky numbers are the positive integ ... 
- lightoj 1097 - Lucky Number(线段树)
		Lucky numbers are defined by a variation of the well-known sieve of Eratosthenes. Beginning with the ... 
随机推荐
- 第一个html程序
			<html><head><title> 表单</title> </head><body><form action=&quo ... 
- (原创)如何在spannableString中使用自定义字体
			最近在做车联网的产品,主打的是语音交互和导航功能,UI给的导航界面可真是够酷炫的.但麻烦的事情也来了,里面的一句话居然用到了三种字体.界面如图所示: 从图中可以看出 500m左前方行驶 居然使用了三种 ... 
- C# 解决DrawImage绘制图片拉伸产生渐变
			ImageAttributes ImgAtt = new ImageAttributes(); ; ImgAtt.SetWrapMode(System.Drawing. ... 
- spring quartz的触发器CrontriggerBean配置
			每一个quartz的CronTrigger表达式分为七个子表达式,每个子表达式之间用空号分割,分别是:秒 分 时 日 月 星期 年.其中年不是必须的,所以CronTrigger最少有六个子表达式. 每 ... 
- html元素
			类型 HTML元素 描述 主窗体元素 <HTML></HTML> 超文本的开始和结束 <HEAD></HEAD> 超文本信息头的开始和结束 <TI ... 
- C# 日期转换函数
			string.Format("{0:d}",dt);//2005-11-5 string.Format("{0:D}",dt);//2005年11月5日 str ... 
- JDBC——数据层DAO
			DAO:Data Access Object DAO 数据层 Service 逻辑业务层 View 视图层 entity 实体层 实现增.删.改.查的数据层 public class EmpDA ... 
- [python]pep8编码规范
			一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ... 
- html表格 第五节
			表格: <html> <head> <title>表格实例</title> </head> <body> <center& ... 
- 国庆第三天2014年10月3日10:21:39,Nutz,WebCollector,jsoup
			(1)做得好,做得快,只能选择一样. (2)时间过得很快,你没法在假期的一天里完成更多的计划.假期全部由自己支配,相对长一点的睡眠,新加入的娱乐(视频或者游戏),你不比在工作中更有效率. (3)每天练 ... 
