poj 3909

我的wa代码 wa到吐血,找不到错。

View Code
 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 using namespace std;
5 __int64 num[1000000],sum,cnt;
6 const __int64 maxn=1000000000100;
7 void dfs(__int64 u,__int64 d)
8 {
9 if(d>12) return;
10 dfs(num[sum++]=u*10+4,d+1);
11 dfs(num[sum++]=u*10+7,d+1);
12 }
13 void Dfs(__int64 t,int pos)
14 {
15 for(int i=pos;i<sum;i++)
16 {
17 __int64 tem=t*num[i];
18 if(tem>0&&tem<=maxn) {
19 num[cnt++]=tem;
20 Dfs(tem,i);
21 }
22 else break;
23 }
24 }
25 void init()
26 {
27 sum=0;
28 dfs(0,1);cnt=sum;
29 sort(num,num+cnt);
30 Dfs(1,0);
31 sort(num,num+cnt);
32 cnt=unique(num,num+cnt)-num;
33 num[cnt]=maxn+1;
34 }
35 __int64 sch(__int64 u)
36 {
37 __int64 low=0,high=cnt,mid;
38 while(low<high)
39 {
40 mid=(low+high)>>1;
41 if(num[mid]<u)
42 low=mid+1;
43 else if(num[mid]>u) high=mid-1;
44 else {high=low=mid; break;}
45 }
46 mid=(high+low)>>1;
47 if(num[mid]!=u)
48 return mid-1;
49 else return mid;
50 }
51 int main()
52 {
53 init();__int64 cas,x,y;
54 scanf("%I64d",&cas);
55 while(cas--)
56 {
57 scanf("%I64d%I64d",&x,&y);
58 printf("%I64d\n",sch(y)-sch(x-1));
59 }
60 return 0;
61 }


网上大牛解法:类似,不过运用了vector

View Code
 1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<algorithm>
5 #include<vector>
6 using namespace std;
7 typedef __int64 lld;
8 vector<lld> lu,res;
9 int tot;
10 void dfs(lld num,int de){
11 if(de==12) return ;
12 lu.push_back(num);
13 dfs(num*10+4,de+1);
14 dfs(num*10+7,de+1);
15 }
16 void dfs1(int x,lld num){
17 for(int i=x;i<lu.size();i++){
18 if(1e12/num<lu[i]) return;
19 res.push_back(num*lu[i]);
20 dfs1(i,num*lu[i]);
21 }
22 }
23 void init(){
24 lu.clear();res.clear();
25 dfs(4,0);
26 dfs(7,0);
27 sort(lu.begin(),lu.end());
28 dfs1(0,1);
29 sort(res.begin(),res.end());
30 tot=unique(res.begin(),res.end())-res.begin();
31 }
32 int main(){
33 init();
34 int t;
35 lld a,b;
36 scanf("%d",&t);
37 while(t--){
38 scanf("%I64d%I64d",&a,&b);
39 int l=lower_bound(res.begin(),res.begin()+tot,a)-res.begin();
40 int r=upper_bound(res.begin(),res.begin()+tot,b)-res.begin();
41 printf("%d\n",r-l);
42 }
43 }

ac代码:

View Code
 1 #include<iostream>
2 #include<algorithm>
3 #include<vector>
4 #include<cstdio>
5 using namespace std;
6 vector<__int64>num;
7 __int64 sum,cnt;
8 const __int64 maxn=1000000000100;
9 void dfs(__int64 u,__int64 d)
10 {
11 if(d>12) return;
12 num.push_back(u*10+4);
13 num.push_back(u*10+7);
14 dfs(u*10+4,d+1);
15 dfs(u*10+7,d+1);
16 }
17 void Dfs(__int64 t,int pos)
18 {
19 for(int i=pos;i<sum;i++)
20 {
21 __int64 tem=t*num[i];
22 if(tem>0&&tem<=maxn) {
23 num.push_back(tem);
24 Dfs(tem,i);
25 }
26 else break;
27 }
28 }
29 void init()
30 {
31 num.clear();
32 dfs(0,1);sum=num.size();
33 sort(num.begin(),num.end());
34 Dfs(1,0);
35 sort(num.begin(),num.end());
36 cnt=unique(num.begin(),num.end())-num.begin();
37 }
38 int main()
39 {
40 init();__int64 cas,x,y;
41 scanf("%I64d",&cas);
42 while(cas--)
43 {
44 scanf("%I64d%I64d",&x,&y);
45 int l=lower_bound(num.begin(),num.begin()+cnt,x)-num.begin();
46 int r=upper_bound(num.begin(),num.begin()+cnt,y)-num.begin();
47 printf("%d\n",r-l);
48 }
49 return 0;
50 }


终于a掉了:

View Code
 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 using namespace std;
5 __int64 num[1000000],sum,cnt;
6 const __int64 maxn=1000000000100;
7 void dfs(__int64 u,__int64 d)
8 {
9 if(d>12) return;
10 dfs(num[sum++]=u*10+4,d+1);
11 dfs(num[sum++]=u*10+7,d+1);
12 }
13 void Dfs(__int64 t,int pos)
14 {
15 for(int i=pos;i<sum;i++)
16 {
17 __int64 tem=t*num[i];
18 if(tem>0&&tem<=maxn) {
19 num[cnt++]=tem;
20 Dfs(tem,i);
21 }
22 else break;
23 }
24 }
25 void init()
26 {
27 sum=0;
28 dfs(0,1);cnt=sum;
29 sort(num,num+cnt);
30 Dfs(1,0);
31 sort(num,num+cnt);
32 cnt=unique(num,num+cnt)-num;
33 num[cnt]=maxn+1;
34 }
35 __int64 sch(__int64 u)
36 {
37 __int64 low=0,high=cnt,mid;
38 while(low<high)
39 {
40 mid=(low+high)>>1;
41 if(num[mid]<u)
42 low=mid+1;
43 else if(num[mid]>u) high=mid-1;
44 else {high=low=mid; break;}
45 }
46 mid=(high+low)>>1;
47 if(num[mid]>u)
48 return mid-1;
49 else return mid;
50 }
51 int main()
52 {
53 init();__int64 cas,x,y;
54 scanf("%I64d",&cas);
55 while(cas--)
56 {
57 scanf("%I64d%I64d",&x,&y);
58 printf("%I64d\n",sch(y)-sch(x-1));
59 }
60 return 0;
61 }

转载于:https://www.cnblogs.com/xuschang-93/archive/2012/03/19/2407121.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值