博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number
阅读量:4628 次
发布时间:2019-06-09

本文共 3003 字,大约阅读时间需要 10 分钟。

A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying two smaller natural numbers.

Now lets define a number NN as the supreme number if and only if each number made up of an non-empty subsequence of all the numeric digits of NN must be either a prime number or 11.

For example, 1717 is a supreme number because 11, 77, 1717 are all prime numbers or 11, and 1919 is not, because 99 is not a prime number.

Now you are given an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100), could you find the maximal supreme number that does not exceed NN?

Input

In the first line, there is an integer T\ (T \leq 100000)T (T≤100000) indicating the numbers of test cases.

In the following TT lines, there is an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100).

Output

For each test case print "Case #x: y", in which xx is the order number of the test case and yy is the answer.

样例输入复制

26100

样例输出复制

Case #1: 5Case #2: 73

题目来源

 题目意思:

给定长度达100的整数,要使 

@这个数是质数或者1

@数字的每一位都必须是1或2或3或5或7

@数字的子序列所组成的数字也必须是质数。

比如317,3和1和7和31和37和17和317都是质数,那么317这个数字就符合标准。

题目思路:

考虑到答案中任意一位都必须是1或质数,可知答案只可能由1、2、3、5、7构成。由于任意两个不为1的数字构成 的两位数一定可以被11整除,所以答案中除1外的数字只能出现一次;1最多出现2次,因为111可以被3整除;而 2、5、7三者一定不会有两者同时出现。因此满足条件的整数不会超过四位,全部预处理出来即可。

方法:先打表

#include
#include
#include
#include
using namespace std;bool isprime(int x){ for (int i=2;i*i<=x;i++) { if (x%i==0) return false; } return true;}bool check(int x){ int a=x%10; int b=x/10%10; int c=x/100; int x1=a*10+b; int x2=a*10+c; int x3=b*10+c; if (x>=1&&x<=9) { if (x==1||x==2||x==3||x==5||x==7) return true; else return false; } else if (x>=10&&x<=99) { if (a!=1&&a!=2&&a!=3&&a!=5&&a!=7) return false; else if (b!=1&&b!=2&&b!=3&&b!=5&&b!=7) return false; else if (isprime(x)==false) return false; else return true; } else if (x>=100&&x<=999) { if (a!=1&&a!=2&&a!=3&&a!=5&&a!=7) return false; else if (b!=1&&b!=2&&b!=3&&b!=5&&b!=7) return false; else if (c!=1&&c!=2&&c!=3&&c!=5&&c!=7) return false; else if (isprime(x1)==false ||isprime(x2)==false||isprime(x3)==false ) return false; else if (isprime(x)==false) return false; else return true; }}int main(){ for (int i=2;i<=999;i++) if (check(i)) cout<
<

得到数值为 :

2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317

所以答案代码为:

 

#include
#include
#include
#include
using namespace std;int main(){ int n,m,j,k,i,T,cas=0,num; char a[1000]; int ans[]={2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317}; cin>>T; getchar(); while (T--) { cas++; num=0; scanf("%s",a); int len=strlen(a); if (len>3) { printf("Case #%d: 317\n",cas); continue; } j=0; for (i=len-1;i>=0;i--) num += (a[i]-'0')*pow(10,j++); //cout<<"num="<
<
=0;i--) { if (num>=ans[i]) { printf("Case #%d: %d\n",cas,ans[i]); break; } } } return 0;}

 

 

 

转载于:https://www.cnblogs.com/Romantic-Chopin/p/10253105.html

你可能感兴趣的文章
composer
查看>>
OpenCV特征点检测——ORB特征
查看>>
ASP.NET性能优化之构建自定义文件缓存
查看>>
apicloud UISearchBar 使用方法
查看>>
【spring+websocket的使用】
查看>>
mongo二维数组操作
查看>>
localStorage之本地储存
查看>>
Archlinux 交换左Ctrl和Cap键
查看>>
#openstack故障处理汇总
查看>>
搜索旋转排序数组 II
查看>>
20、docker swarm
查看>>
psp工具软件前景与范围文档
查看>>
day06-三元表达式
查看>>
C# DateTime.Now详细用法
查看>>
Php中"{}"大括号的用法总结(转)
查看>>
JavaScript内存优化
查看>>
BZOJ1059: [ZJOI2007]矩阵游戏(二分图匹配)
查看>>
P3385 【模板】负环
查看>>
URI、URL 和 URN的区别
查看>>
根据表达式序列(前缀、中缀、后缀)构建表达式树
查看>>