題目描述
到x星球旅行的游客都被發給一個整數,作為游客編號。
x星的國王有個怪癖,他只喜歡數字3,5和7。
國王規定,游客的編號如果只含有因子:3,5,7,就可以獲得一份獎品。
前10個幸運數字是:3 5 7 9 15 21 25 27 35 45,因而第11個幸運數字是:49
小明領到了一個幸運數字 59084709587505。
去領獎的時候,人家要求他准確說出這是第幾個幸運數字,否則領不到獎品。
請你幫小明計算一下,59084709587505是第幾個幸運數字。
x星的國王有個怪癖,他只喜歡數字3,5和7。
國王規定,游客的編號如果只含有因子:3,5,7,就可以獲得一份獎品。
前10個幸運數字是:3 5 7 9 15 21 25 27 35 45,因而第11個幸運數字是:49
小明領到了一個幸運數字 59084709587505。
去領獎的時候,人家要求他准確說出這是第幾個幸運數字,否則領不到獎品。
請你幫小明計算一下,59084709587505是第幾個幸運數字。
輸出
輸出一個整數表示答案
題解:幸運數字=3x * 5y * 7z ,取一個優先隊列,每次把符合條件的數加進去,從小到大依次把隊列的每一個數去和3,5,7相乘,得到符合條件得數在加進隊列,直到找到幸運數字結束
#include<iostream> #include<string> #include<algorithm> #include<math.h> #include<string.h> #include<map> #include<stack> #include<queue> #include<set> #define ll long long using namespace std; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } ll a[3]={3,5,7}; priority_queue<ll,vector<ll>,greater<ll> >p; map<ll,int>mp; int main() { p.push(1); int ans=0; while(1) { ll x=p.top(); p.pop(); if(x==59084709587505) { cout<<ans<<endl; break; } for(int i=0;i<3;i++) { ll temp=x*a[i]; if(mp.count(temp)==0) { mp[temp]=1; p.push(temp); } } ans++;//注意位置 } return 0; }
每次二分找到一個比上次乘3,5,7大得數繼續去乘3,5,7,直到找到符合條件得數
#include<iostream> #include<string> #include<algorithm> #include<math.h> #include<string.h> #include<map> #include<stack> #include<queue> #include<set> #define ll long long using namespace std; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } ll a[3]={3,5,7}; set<ll>s; int main() { ll x=1; while(1) { if(x==59084709587505) break; for(int i=0;i<3;i++) { ll temp=x*a[i]; if(temp<=59084709587505)//注意要取等 s.insert(temp); } x=*s.upper_bound(x);//指針,在集合s中找大於x 的第一個數 } cout<<s.size()<<endl; return 0; }