• bzoj 1053 [HAOI2007]反素数ant——关于质数的dfs / 打表


    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053

    写了个打表程序。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int lst=1,cnt,N=2e9,fx=2,knt;
    int main()
    {
        for(int i=2;i<=N;i+=fx)
        {
            cnt=1;int k=i;
            for(int j=2;j*j<=k;j++)
            {
                int ct=0;
                if(k%j==0)
                {
                    if(j!=2&&(j&1)==0){cnt=2;break;}
                    while(k%j==0)k/=j,ct++;
                }
                cnt*=ct+1;
            }
            if(k>1)cnt<<=1;
            if(cnt>lst)printf("%d  ",i),lst=cnt,knt++;
            if(i==60)fx=10;
            if(i==2520)fx=120;
            if(i==1441440)fx=240;
        }
        return 0;
    }
    View Code

    打了个表过了……

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n;
    int c[75]={1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,
    10080,15120,20160,25200,27720,45360,50400,55440,83160,110880,166320,221760,277200,
    332640,498960,554400,665280,720720,1081080,1441440,2162160,2882880,3603600,4324320,
    6486480,7207200,8648640,10810800,14414400,17297280,21621600,32432400,36756720,
    43243200,61261200,73513440,110270160,122522400,147026880,183783600,245044800,
    294053760,367567200,551350800,698377680,735134400,1102701600,1396755360};
    int main()
    {
        scanf("%d",&n);
        for(int i=67;i>=0;i--)
            if(c[i]<=n)
            {
                printf("%d
    ",c[i]);break;
            }
        return 0;
    }
    View Code

    然而实际上是dfs。

    首先要发现质因数数量一定时越小的质因数应该越多。如果有一个质因数较大而较多,可以把它的数量与一个较小的质因数的数量换一下,这样算出来的约数个数不变,而答案更优。

    然后发现2*3*5*7*11*13*17*19*23*29大于2e9。因为上面一行的性质,所以用到的最大的质数是23。

    然后就能爆搜了。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    using namespace std;
    int n,mx,ans,pri[15]={2,3,5,7,11,13,17,19,23};
    void dfs(int ps,int cnt,int lst,ll w)
    {
        if(cnt>mx)mx=cnt,ans=n+5;
        if(cnt==mx)ans=min(ans,(int)w);
        if(ps>8)return;//放在这! 
        for(int i=0;i<=lst&&w<=n;i++,w*=pri[ps])//为什么不能从1开始? 
            dfs(ps+1,cnt*(i+1),i,w);
    }
    int main()
    {
        scanf("%d",&n);
        dfs(0,1,32,1);
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    HDU 4069 Squiggly Sudoku
    SPOJ 1771 Yet Another NQueen Problem
    POJ 3469 Dual Core CPU
    CF 118E Bertown roads
    URAL 1664 Pipeline Transportation
    POJ 3076 Sudoku
    UVA 10330 Power Transmission
    HDU 1426 Sudoku Killer
    POJ 3074 Sudoku
    HDU 3315 My Brute
  • 原文地址:https://www.cnblogs.com/Narh/p/9364523.html
Copyright © 2020-2023  润新知