• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

练习赛 又是质数 dfs

又是质数

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 74   Accepted Submission(s) : 8

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

给定若干质数。输出满足条件的所有整数,这些整数仅能由这些质数的乘积构成(不能使用其他质数)。注意1总是符合规则的,因为它不依赖于任何质数。

Input

输入包含多组数据。
每组数据的第一行是整数N(1≤N≤10) — 质数的数目。第二行是N个空格分开的质数2 ≤ P1< P2 < P3 <... <PN < 10000.第三行是两个整数 X 和 Y (1 ≤ X ≤ Y < 2^31).
输入是以包含0的一行结束。

Output

以升序方式输出区间[X, Y ] 中的所有数,其质数因子均来自输入集合中给定的Pi
.两两之间用逗号隔开. 如果不存在满足条件的数,则输出 “none” .

Sample Input

1
3
1 12
2
2 3
10 20
3
2 3 5
20 30
1
17
20 30
0

Sample Output

1,3,9
12,16,18
20,24,25,27,30
none

Author

hnust_loser
 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <vector>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 using namespace std;
11 #define maxn 100005
12 #define mod 1000000007
13 #define INF 0x7fffffff
14 //#define ll long long
15 #define ll __int64
16 int n,m,l,r,k;
17 int a[20];
18 int ans[maxn];
19 void dfs(int u,ll s){
20     if(s>=l&&s<=r)ans[k++]=s;
21     if(s>r)return;
22     for(int i=u;i<n;i++){
23         s*=a[i];
24         dfs(i,s);
25         s/=a[i];
26     }
27 }
28 int main(){
29     ll s;
30     while (~scanf("%d",&n)&&n){
31         for(int i=0;i<n;i++)
32             scanf("%d",&a[i]);
33         scanf("%d%d",&l,&r);
34         k=0;
35         for(int i=0;i<n;i++){
36             s=a[i];
37             dfs(i,s);
38         }
39         int i=0;
40         if(k==0&&l>1){printf("none\n");continue;}
41         sort(ans,ans+k);
42         if(l<=1&&r>=1)printf("1");
43         else {printf("%d",ans[0]);i++;}
44         for(;i<k;i++)printf(",%d",ans[i]);
45         printf("\n");
46     }
47     return 0;
48 }
View Code 2013-09-25 12:14:46

 

posted @ 2013-09-25 12:02  HaibaraAi  阅读(132)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3