Codeforces 225b well-known numbers

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/225/ B

Idea: maintain a queue with a length of K and use the number kK-The number of bonacci instances is equal to the first K.K-Calculate the sum of bonacci numbers for all values not greater than S.K-Bonacci number, which can be recorded with a set, and then the number of S is closest to each greedy operation.

View code

 1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <iostream> 8 #include <cmath> 9 #include <cstring>10 #include <algorithm>11 #include <string>12 #include <vector>13 #include <queue>14 #include <stack>15 #include <map>16 #include <set>17 using namespace std;18 19 typedef long long ll;20 const int INF = 2147483647;21 22 set<int>su;23 void cal(int s,int k)24 {25     queue<int>q;26     q.push(1);27     int sum=1;28     su.insert(sum);29     ll tmp;30     while(1)31     {32         if(q.size() == k)33         {34             su.insert(sum);35             tmp = (ll)(2*sum )- q.front();36             if(tmp > s)return;37             su.insert(tmp);38             q.push(sum);39             sum = tmp;40             q.pop();41         }42         else43         {44             su.insert(sum);45             tmp = (ll)sum * 2;46             if(tmp > s)return ;47             q.push(sum);48             sum = tmp;49 50         }51     }52     return;53 }54 int main()55 {56     #ifndef ONLINE_JUDGE57     freopen("in","r",stdin);58     #endif59     int s,k;60     while(~scanf("%d%d",&s,&k))61     {62         cal(s,k);63         int S[35]={0},top=1;64         while(s)65         {66             set<int>::iterator it = su.lower_bound(s);67             if(it != su.begin() && *it != s)it--;68             S[top++]=*it;69             s-=*it;70         }71         printf("%d\n",top);72         for(int i=top-1;i>=0;i--)printf("%d ",S[i]);73         puts("");74     }75     return 0;76 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.