您的位置:首页 > 理论基础 > 计算机网络

2016 ACM/ICPC 青岛区域赛网络赛 1001 I Count Two Three(打表+二分)

2016-09-18 10:35 429 查看

I Count Two Three

[align=center]Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3582    Accepted Submission(s): 1094[/align][align=left]Problem Description[/align]I will show you the most popular board game in the Shanghai Ingress Resistance Team.It all started several months ago.We found out the home address of the enlightened agent Icount2three and decided to draw him out.Millions of missiles were detonated, but some of them failed.After the event, we analysed the laws of failed attacks.It's interesting that the i-thattacks failed if and only if ican be rewritten as the form of 2a3b5c7dwhich a,b,c,dare non-negative integers.At recent dinner parties, we call the integers with the form2a3b5c7d"I Count Two Three Numbers".A related board game with a given positive integernfrom one agent, asks all participants the smallest "I Count Two Three Number" no smaller thann. [align=left]Input[/align]The first line of input contains an integert (1≤t≤500000),the number of test cases. ttest cases follow. Each test case provides one integern (1≤n≤109). [align=left]Output[/align]For each test case, output one line with only one integer corresponding to the shortest "I Count Two Three Number" no smaller thann. [align=left]Sample Input[/align]
1011113123123412345123456123456712345678123456789 [align=left]Sample Output[/align]
11214125125012348123480123480012348000123480000 题意:给出一个数n,求一个数m,要求m为能够表示成m=2a3b5c7d,并且离n最近,比如n=11,距离11最近且能表示成这种形式的数为12,,12=2^2*3^1*5^0*7^0,, 思路:首先想到打表,找出1到1e9中满足条件的所有可能,,,但是求结果的时候要判断过程中如果数大于1e9,则Break,,,结果一共有5000多个满足条件的数,,,最后求解的时候扫一遍就行了,,激动地敲完代码提交了一次,超时!!以为暴力的时候超时,各种改代码,,还是超时,,最后竟然死在求解那,,不能简单地扫一遍,,10^9数量级,竟然没想到,,,最后看到题解,,用lower_bound()解决的。最后附上一条链接(<alogrithm>中常用函数)alogrithm中常用函数
以下AC代码(参考)
#include<stdio.h>#include<math.h>#include<stdlib.h>#include<algorithm>using namespace std;long long a[1000000];long long pow(int x,int num){long long ans=1;for(long long i=1;i<=num;i++) ans=ans*x;return ans;}int main(){int i,j,k,l;long long t,n;int index=0;for(i=0;i<=31;i++){for(j=0;j<=19;j++){for(k=0;k<=12;k++){for(l=0;l<=11;l++){long long flag=pow(2,i);if(flag>1e9) break;flag*=pow(3,j);if(flag>1e9) break;flag*=pow(5,k);if(flag>1e9) break;flag*=pow(7,l);if(flag>1e9) break;a[index++]=flag;}}}}sort(a,a+index);scanf("%I64d",&t);while(t--){int temp;scanf("%I64d",&n);temp=lower_bound(a,a+index,n)-a;printf("%I64d\n",a[temp]);}return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: