zoukankan      html  css  js  c++  java
  • 数的计数(number)

    数的计数(number)

    题目描述

    我们要求找出具有下列性质数的个数(包含输入的自然数n),先输入一个自然数n(n≤1000),然后对此自然数按照如下方法进行处理:
    (1)不作任何处理;
    (2)在它的左边加上一个自然数,但该自然数不能超过原数的一半;
    (3)加上数后,继续按此规则进行处理,直到不能再加自然数为止。

    输入

    一个正整数n。

    输出

    符合以上性质的数的个数。

    样例输入

    6
    

    样例输出

    6
    

    提示


    样例说明:满足条件的数为6,16,26,36,126,136。

    分析:暴搜会超时,所以考虑dp,dp[i]=sum[i/2]+1,sum[i]=sum[i-1]+dp[i],维护一个前缀和数组就好了

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e3+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,dp[maxn],a[maxn];
    void init()
    {
        for(int i=1;i<=1000;i++)
        {
            dp[i]=a[i/2]+1;
            a[i]=a[i-1]+dp[i];
        }
    }
    int main()
    {
        int i,j,k,t;
        init();
        scanf("%d",&n);
        printf("%d
    ",dp[n]);
        //system ("pause");
        return 0;
    }
  • 相关阅读:
    深入剖析C#的多态
    .NET多线程编程:多任务和多线程
    .Net类库中实现的HashTable
    用C#编写ActiveX控件
    用微软.NET架构企业解决方案 学习笔记(四)业务层
    SQL事务
    WCF基础知识问与答
    在.NET环境中使用单元测试工具NUnit
    圣殿骑士博客转载系列
    系统架构师学习笔记_第十二章
  • 原文地址:https://www.cnblogs.com/dyzll/p/5675286.html
Copyright © 2011-2022 走看看