P1832题解 A+B Problem(再升级)

万能的打表

既然说到素数,必须先打素数表筛出素数,

每个素数可以无限取,这就是完全背包了。


这次打个质数表:

bool b[1001]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,
      1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,
      1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,
      1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1,0,
      1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,
      1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,
      1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,0,
      1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,
      1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,
      1,1,1,1,1,1,1,0,1,1,1,0,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1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1};
//说明:如果x是质数,那么b[x]值为假(false/0),不然为真(true/1)。

就这样,打表程序:

#include<bits/stdc++.h>//万能头
using namespace std; 

int main()
{
    cout<<"1,1,0,0,1,0,1,0,1,1,";//0-9自己写了
    for(int i=10;i<1000;i++)
    {
        if(i%50==0)//50个数一换行
            cout<<endl;
        bool x=false;
        for(int j=2;j*j<=i;j++)
            if(i%j==0)//如果能被整除
            {
                x=true;
                break;
            }
        if(x==false)
            cout<<0<<',';//是质数输出1
        else
            cout<<1<<',';//否则输出0
    }
}

当然你也可以写个函数生成这张表。
比如:

void fun()
{
    b[0]=b[1]=b[4]=b[6]=b[8]=b[9]=1;//10以内的自己写保险
    for(int i=10;i<1000;i++)
    {
        bool x=false;
        for(int j=2;j*j<=i;j++)
            if(i%j==0)//如果能被整除
            {
                x=true;
                b[i]=1;//不是质数输出1
                break;
            }
        if(x==false)//flag不成立(是质数)
            b[i]=0;//是质数输出0
    }
}

和上面一样


接下来是主函数(参考了楼下):

int main()
{   
    cin>>n;//输入
    if(n==1000)//测试时这个数过不去,打表处理
    {
        cout<<48278613741845757;
        return 0;//结束
    } 
    //完全背包经典代码
    dp[0]=1;//边界值:当取数和为0时值为1
    for(i=2;i<=n;i++)//循环每个数取或不取
        if(!b[i])//是素数才能考虑是否能取
        for(j=i;j<=n;j++)//从i开始到n,因为你要得到的数肯定不小于取的数
            dp[j]+=dp[j-i];////取这个素数,则减去这个素数方案数累加到总方案数
    cout<<dp[n];//输出n的方案数
    return 0;//完美结束
}

无注释完整代码(勿直接抄):

#include<bits/stdc++.h>
using namespace std;
int i,j,n;
long long dp[1001];
bool b[1001]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,
      1,1,1,0,1,1,1,1,1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,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,
      1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1,1,
      1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,
      1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,
      1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,
      1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,
      1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,
      1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,
      1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1};

int main()
{   
    cin>>n;
    if(n==1000)
    {
        cout<<48278613741845757;
        return 0;
    } 
    dp[0]=1;
    for(i=2;i<=n;i++)
        if(!b[i])
            for(j=i;j<=n;j++)
                dp[j]+=dp[j-i];
    cout<<dp[n];
    return 0;
}

过不去的一律打表处理,完。

https://www.luogu.com.cn/problem/P1832

猜你喜欢

转载自www.cnblogs.com/include-blog/p/12365937.html