首页 > 其他分享 >codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)

codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)

时间:2023-04-23 21:32:13浏览次数:47  
标签:契数 题目 Well codeforces 构造 225B 菲波 include col


题目链接:

codeforces 225B


题目大意:

定义f(k,n)为类似菲波那契数推导,只不过变为前k项的和,然后给出一个数s,利用k-菲波那契数构造出一个不重复的集合的元素和为s,集合的规模大于1


题目分析:

  • 首先因为菲波那契数的增长速度快的吓人,所以给的数据范围109很快就能达到,我们得到O(n)的构造出所有的可能用到的k-菲波那契数是可行的。
  • 然后就是构造如何构造的问题了。
  • 因为题目保证解一定存在,那么我们考虑如何构造一组可行解,采取的是贪心的方法,先选取能够选择的元素中最大的。
  • 如果答案的集合只有1个元素,那么我们添上0保证得到的解的规模
  • f(k,n)−f(k,n−1)=f(k,n−1)−f(k,n−k−1)⇒f(k,n)−f(k,n−k−1)=2f(k,n−1)⇒f(k,n)≤2f(k,n−1)
    这样如果对于给定数s的一个因数是数列中的数位置为n,倍数等于2,那么我们通过选取f(k,n+1)能够保证有合法解避免这种同一个数出现多次的冲突,找到最大的那个数,因为那个数不可能出现当前的s是它的两倍,因为s

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#define MAX 100007

using namespace std;

typedef long long LL;

LL a[MAX];
int s,k,n;
multiset<int,greater<int> > col;
vector<int> ans;

int main ( )
{
    while (~scanf ( "%d%d" , &s , &k ) )
    {
        col.clear();
        ans.clear();
        memset ( a , 0 , sizeof ( a ) );
        n = 1;
        a[1] = 1;
        a[0] = 0;
        while ( a[n]-a[n-1] <= s )
        {
            int l = max ( n-k , 0 );
            a[n+1] = a[n]-a[l];
            col.insert ( a[n+1] );
            a[n+1] += a[n];
            n++;
        }
        multiset<int>::iterator it;
        /*cout << "------------------------------" << endl;
        //multiset<int>::iterator it;
        for ( it = col.begin() ; it != col.end() ; it++ )
            cout << *it << endl;
        cout << "---------------------------" << endl;*/
        while ( s )
        {
            it = col.lower_bound ( s );
            s -= *it;
            ans.push_back ( *it );
            col.erase ( it );
        }
        while ( k!= 1 && ans.size() < 2 ) ans.push_back ( 0 );
        printf ( "%d\n" , (int)ans.size() );
        for ( int i = 0 ; i < ans.size() ; i++ )
            printf ( "%d " , ans[i] );
        puts ("");
    }
}


标签:契数,题目,Well,codeforces,构造,225B,菲波,include,col
From: https://blog.51cto.com/u_7936627/6218759

相关文章

  • Quasi Binary---codeforces
    #QuasiBinary##题面翻译**题目描述**给出一个数n,你需要将n写成若干个数的和,其中每个数的十进制表示中仅包含0和1。问最少需要多少个数**输入输出格式**输入格式:一行一个数n(1≤n≤10^6)输出格式:最少的数的个数,并给出一种方案。##题目描述Anumberiscalledquasib......
  • Codeforces Round 866 (Div. 2)(A~C)
    目录A.Yura'sNewName题意思路代码B.JoJo'sIncredibleAdventures题意思路代码C.ConstructiveProblem题意思路代码A.Yura'sNewName题意在字符串\(s\)中添加"_"或者"^",使得\(s\)中的任意字符都必须为"_"或者"^^"的一部分,求最少要添加的字符数量思路当字符串......
  • Codeforces Round 689 (Div. 2, based on Zed Code Competition)D.Divide and Summari
    题意:我们给定包含n个正整数的数组,我们可以对这个数组执行一些操作之后,可以让数组内元素的和成为我们想要的数。我们对数组的执行操作一共分为三个步骤,第一个步骤是我们首先计算出数组的中间值mid。这里mid的定义不是中位数也不是均值,而是最大值和最小值的均值。也就是mid=(min......
  • codeforces #864 div2 B
    GCDPartition这道题首先要解决一个问题,要把区间分成几块,可以证明分成两块是更优首先我们假设把区间分成了m(>=2)块b1,b2,b3,...,bm,则答案是gcd(b1,b2,b3,...,bm),则b1,b2是gcd(b1,b2,b3,...,bm)的倍数,那么b1+b2也是gcd(b1,b2,b3,...,bm)的倍数,所以gcd(b1,b2,......
  • Educational Codeforces Round 147 (Rated for Div. 2)
    EducationalCodeforcesRound147(RatedforDiv.2)链接EducationalCodeforcesRound147(RatedforDiv.2)A题如果第一位数是0,直接打印0如果第一位数是'?',有9个数可以选择,如果其他位数是'?',有10中情况选择,相乘就可以了#include<iostream>#include<algo......
  • CodeForces 34B Sale
    B- SaleTimeLimit:2000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice CodeForces34BDescriptionOnceBobgottoasaleofoldTVsets.Therewere n TVsetsatthatsale.TVsetwithi......
  • C. Table Decorations(Codeforces)(规律)
    C.TableDecorationstimelimitpertestmemorylimitpertestinputoutputr red, g greenand b blueballoons.Todecorateasingletableforthebanquetyo......
  • CodeForces 34A Reconnaissance 2
     Reconnaissance2TimeLimit:2000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice CodeForces34ADescriptionn soldiersstandinacircle.Foreachsoldierhisheight ai isknown.Areco......
  • codeforces round The Monster and the Squirrel 529B (数学规律)
    TheMonsterandtheSquirrelTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionArithemonsteralwayswakesupveryearlywiththefirstrayofthesunandthefirstthingshedoesisfeedinghersqu......
  • Codeforces Round 850 (Div. 2, based on VK Cup 2022 - Final Round) E. Monsters (h
    传送门详细题解传送门  抄的ygg代码,向在这里说一下刚开始没看懂的部分。  求答案的时候是把所有的当前为止的所有数值加起来减去一个从1开始并且公差为1的等差数列的前size项和。其中size是当前最多能用到哪个位置,满足前size项能构成1,2,3,....,sz这样的形式。  假设我们......