整数划分问题

博客给出了整数划分相关的题目链接,包括将整数划分为不同整数的划分方法、划分为若干个整数(可相同)的方法以及划分为2的幂的题目,还给出了一篇相关文章的转载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.51nod.com/Challenge/Problem.html#!#problemId=1201

将整数划分为不同的整数的划分方法

#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+8;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+7;
const ULL base=1e7+7;
using namespace std;
int dp[50008][408];
int main(){
    int n;
    scanf("%d",&n);
    dp[1][1]=1;
    for(int i=2;i<=n;i++){
        for(int j=1;j*(j+1)/2<=i;j++){
            dp[i][j]=(dp[i-j][j]+dp[i-j][j-1]);
            dp[i][j]%=mod;
        }
    }
    LL ans=0;
    for(int j=1;j*(j+1)/2<=n;j++){
        ans+=dp[n][j];
        ans%=mod;
    }
    cout<<ans<<endl;
}

http://www.51nod.com/Challenge/Problem.html#!#problemId=1259 

将整数划分为若干个整数(可以有相同)的方法

#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+8;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+7;
const ULL base=1e7+7;
using namespace std;
LL dp[50008];
int fiv[50008];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;fiv[i*2-2]<50008;i++){///五边形数
        fiv[i*2-1]=i*(3*i-1)/2;
        fiv[i*2]=i*(3*i+1)/2;
    }
    dp[0]=1;
    dp[1]=1;
    dp[2]=2;
    dp[3]=3;
    dp[4]=5;
    dp[5]=7;
    for(int i=6;i<=n;i++){
        int q=1;
        for(int k=1;fiv[k]<=i;k++){
            dp[i]+=q*dp[i-fiv[k]];
            dp[i]%=mod;
            if(k%2==0){
                q=-q;
            }
        }
    }
    if(dp[n]<0) dp[n]+=mod;
    cout<<dp[n]<<endl;
}

 整数划分为2的幂

http://www.51nod.com/Challenge/Problem.html#!#problemId=1383

#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+8;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+7;
const ULL base=1e7+7;
using namespace std;
LL dp[maxn];
int main(){
    int n;
    scanf("%d",&n);
    dp[0]=1;
    for(int i=1;i<=n;i++){
        if(i&1){
            dp[i]=dp[i-1];
        }
        else{
            dp[i]=dp[i-1]+dp[i/2];
        }
        dp[i]%=mod;
    }
    if(dp[n]<0) dp[n]+=mod;
    cout<<dp[n]<<endl;
}

 

转载于:https://www.cnblogs.com/Profish/p/11190698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值