Dollar Dayz S(DP+高精)

Dollar Dayz S(DP+高精)

题目链接:https://www.luogu.com.cn/problem/P6205

在这里插入图片描述

有K种工具,由题目的意思可以看出每种的购买次数工具不限,也就是一个完全背包求方案数的问题

但是如果只是一个简单的完全背包代码放上去会发现:
在这里插入图片描述
附上代码:

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}


const int N = 1e3+5;
int dp[N];

int main()
{
    
    
	int n,k;
	n=read(); k=read();
	dp[0]=1; 
	for(int i=1;i<=k;i++)
		for(int j=i;j<=n;j++)	dp[j]+=dp[j-i];
	cout<<dp[n]<<endl;
	return 0;
}

这时候发现并没有这么简单,将极限数据输入会发现答案为负数
然后开个long long?会发现依然WA三个点

只好有高精过了这道题

附上vector高精和数组高精代码

vector实现高精:

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}
vector<int> add(vector<int> &A,vector<int> &B)
{
    
    
	int la=A.size(),lb=B.size();
	vector<int> C;
	int temp=0;
	for(int i=0;i<la||i<lb;i++)
	{
    
    
		if(i<la)	temp+=A[i];
		if(i<lb)	temp+=B[i];
		C.push_back(temp%10);
		temp/=10;
	}
	if(temp)	C.push_back(temp);
	return C;
}
const int N = 1e3+5;
vector<int> dp[N];
int main()
{
    
    
	int n,k;
	n=read(); k=read();
	
	dp[0].push_back(1);
	
	for(int i=1;i<=k;i++)
		for(int j=i;j<=n;j++)	dp[j]=add(dp[j],dp[j-i]);
	for(int i=dp[n].size()-1;~i;i--)	cout<<dp[n][i];
	return 0;
}

数组实现高精:

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}

const int N = 1e3+5;
int dp[N][N];

void add(int a[],int b[])
{
    
    
	a[0]=max(a[0],b[0]);
	for(int i=1;i<=a[0];i++)	a[i]+=b[i];
	for(int i=1;i<=a[0];i++)	a[i+1]+=a[i]/10, a[i]%=10;
	if(a[a[0]+1])	a[0]++;
}
int main()
{
    
    
	int n,k;
	n=read(); k=read();
	dp[0][0]=1; dp[0][1]=1;//0位储存长度 
	for(int i=1;i<=k;i++)
		for(int j=i;j<=n;j++)
			add(dp[j],dp[j-i]);
	for(int i=dp[n][0];i>0;i--)	cout<<dp[n][i];
	return 0;
}


后来进了题解区才发现还有__int128这一说
代码如下

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}

//因为cout与printf都不支持直接输出__int128,所以用快写 
inline void write(__int128 x)
{
    
    
	if(x<0)	putchar('-'),x=-x;
	if(x>9)	write(x/10);
	putchar(x%10+'0');
}

typedef __int128 bigll;
const int N = 1e3+5;

bigll dp[N]={
    
    1};

int main()
{
    
    
	int n,k;
	n=read(); k=read();
	for(int i=1;i<=k;i++)
		for(int j=i;j<=n;j++)	dp[j]+=dp[j-i];
	write(dp[n]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_50815157/article/details/113352249
今日推荐