zoukankan      html  css  js  c++  java
  • P1028 数的计算

    P1028 数的计算

    递推做法:

    我们先列出前几项,找规律

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
    1 1 2 2 4 4 6 6 10 10 14 14 20 20 26

    于是乎我们惊奇的发现

    f [ 1 ] = 1

    f [ 2 ] = f [ 1 ] + 1 

    f [ 3 ] = f [ 1 ] + 1

    f [ 4 ] = f [ 1 ] + f [ 2 ] + 1 

    f [ 5 ] = f [ 1 ] + f [ 2 ] + 1 

    f [ 6 ] = f [ 1 ] + f [ 2 ] + f [ 3 ] + 1 

     

    也就是 f [ n ] = ∑ f [ i ]  ( i = 1 , 2 , 3 .... n / 2 )    + 1

    代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    
    using namespace std;
    
    int ans[1005],n;
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i/2;j++)
              ans[i]+=ans[j];
            ans[i]++;
        }
          
        printf("%d",ans[n]);
        
        return 0;    
        
    }

    DP做法:

    我们先列出前几项,找规律

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
    1 1 2 2 4 4 6 6 10 10 14 14 20 20 26

    于是乎我们又一次惊奇的发现

    1.   f [ 1 ] = f [ 0 ]       f [ 3 ] = f [ 2 ]        f [ 5 ] = f [ 4 ] 

          即   if ( i % 2 == 1 )    f [ i ] = f [ i - 1 ] 

    2.  f [ 2 ] = f 1 + f 0          f [ 4 ] = f 3 + f 2          f [ 6 ] = f 5 + f 3          f [ 8 ] = f 7 + f 4       

          即  f [ i ] = f [ i - 1 ] + f [ i / 2 ]      ( i % 2 == 0 )

               若(i % 2 == 1)  f [ i ] = f [ i - 1 ] 

    代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    
    using namespace std;
    
    int f[1005],n;
    
    int main()
    {
        scanf("%d",&n);
        
        f[0]=1;f[1]=1;
        for(int i=2;i<=n;i++)
        {
            if(i%2==1) f[i]=f[i-1-1]+f[(i-1)/2];
            else f[i]=f[i-1]+f[i/2];
        }
          
        printf("%d",f[n]);
        
        return 0;    
        
    }
  • 相关阅读:
    POJ 3680_Intervals
    POJ 3680_Intervals
    POJ 3686_The Windy's
    POJ 3686_The Windy's
    Codeforces 629C Famil Door and Brackets
    Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
    Codeforces 629D Babaei and Birthday Cake(线段树优化dp)
    Codeforces 628F Bear and Fair Set
    18.04.26 魔兽世界终极版
    18.4.20 STL专项练习8选6
  • 原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/10963600.html
Copyright © 2011-2022 走看看