zoukankan      html  css  js  c++  java
  • 6.28数论测试

    (hao.cpp/c/pas)

    【问题描述】
    从1− N中找一些数乘起来使得答案是一个完全平方数,求这个完全平方数
    最大可能是多少。
    【输入格式】
    第一行一个数字N。
    【输出格式】
    一行一个整数代表答案对100000007取模之后的答案。
    【样例输入】
    7
    【样例输出】
    144
    【样例解释】
    但是塔外面有东西。
    【数据规模与约定】
    对于20%的数据,$1leq Nleq 100$。
    对于50%的数据,$1leq Nleq 5000$。
    对于70%的数据,$1leq Nleq 10^5$。
    对于100%的数据,$1leq Nleq 5 imes 10^6$。

    思路分析:

    1.完全平方数的性质:

    egin{equation*}对于任意完全平方数n,总能写成n=a^bcdot c^dcdotcdots y^z(a, c, f, cdots , y均为质数,b, d, e, cdots , z 均为偶数)的形式.end{equation*}

    由这个性质,不妨对N!进行质因数分解.分解成近似完全平方数的形式(比完全平方数的分解稍弱):

    将N!展开:

    egin{equation}N!=1 imes 2 imes 3 imes cdots imes Nend{equation}

    然后对展开式中的每一项进行质因数分解,分解成以下形式:

    egin{equation}s_i=a^bcdot c^dcdotcdots cdot y^zend{equation}

    再代入展开式,得到以下形式:

    egin{equation}N!=a^bcdot c^dcdotcdots cdot y^zend{equation}

    取出(3)式中所有次数为偶数的因子(本身就是完全平方数),乘起来,记作$A$;

    对于(3)式中其他的次数不为偶数的因子(不能写成完全平方数),需要转化成完全平方数的形式的话,有两种方式:

    a.次数+某个奇数.

    b.次数-某个偶数.

    明显地,次数+某个奇数无法实现(完全不符合题意).只能次数-某个偶数以保证乘积为完全平方数.

    又因为要使得完全平方数最大,最小的奇数为1.

    所以对于(3)式中其他的次数不为偶数的因子,我们处理的策略是对每一项的所有次数-1,然后乘起来,记作$B$;

    然后计算$A cdot B;mod;100000007$即可.

    以上为数学做法.

    但是考虑到在C++会爆unsigned long long,所以我们决定使用__int1024(怎么会有)。

    正解应该是在(2)式->(3)式的每一项进行统计,统计完成后按照以上方法套快速幂,边乘边模即可.

    AC代码:

      1 #include<cstdio>
      2 #define _____________ 5000000
      3 #define ____________ 100000007
      4 #define _____________________ for
      5 #define ______________________ while
      6 #define _______________________ if
      7 #define ________________________ return
      8 #define _________________________ main
      9 #define __________________________ freopen
     10 #define ___________________________ "hao.in"
     11 #define ____________________________ "r"
     12 #define _____________________________ stdin
     13 #define ______________________________ "hao.out"
     14 #define _______________________________ "w"
     15 #define ________________________________ stdout
     16 #define _________________________________ scanf
     17 #define __________________________________ printf
     18 #define ___________________________________ "%d"
     19 #define ____________________________________ {
     20 #define _____________________________________ }
     21 #define ______________________________________ ,
     22 #define _______________________________________ ;
     23 #define ________________________________________ [
     24 #define _________________________________________ ]
     25 #define __________________________________________ (
     26 #define ___________________________________________ =
     27 #define ____________________________________________ )
     28 #define _____________________________________________ 1
     29 #define ______________________________________________ ++
     30 #define _______________________________________________ 2
     31 #define ________________________________________________ *
     32 #define _________________________________________________ %
     33 #define __________________________________________________ &
     34 #define ___________________________________________________ /
     35 #define ____________________________________________________ <=
     36 #define _____________________________________________________ +=
     37 #define ______________________________________________________ >>=
     38 #define _______________________________________________________ +
     39 #define ________________________________________________________ /=
     40 #define _________________________________________________________ 0
     41 #define __________________________________________________________ !
     42 #define ___________________________________________________________ long
     43 #define ____________________________________________________________ typedef
     44 #define _____________________________________________________________ int
     45 #define ______________________________________________________________ void
     46 #define _______________________________________________________________ using
     47 #define ________________________________________________________________ namespace
     48 #define _________________________________________________________________ std
     49 #define __________________________________________________________________ += 
     50 _______________________________________________________________ ________________________________________________________________ _________________________________________________________________ _______________________________________
     51 ____________________________________________________________ ___________________________________________________________ ___________________________________________________________ ____________________ _______________________________________
     52 ____________________________________________________________ _____________________________________________________________ ___________________ _______________________________________
     53 ____________________________________________________________ ______________________________________________________________ __________________ _______________________________________
     54 ___________________ _________ ______________________________________ ___ _______________________________________
     55 ___________________ ____ ________________________________________ _____________ _________________________________________ _______________________________________
     56 ___________________ _ ________________________________________ _____________ _________________________________________ _______________________________________
     57 ___________________ ___________ ________________________________________ _____________ _________________________________________ _______________________________________
     58 ____________________ _____ ___________________________________________ _____________________________________________ _______________________________________
     59 __________________ ______ __________________________________________ ____________________________________________
     60 ____________________________________
     61     _____________________ __________________________________________ ___________________ _______ ___________________________________________ _______________________________________________ _______________________________________ _______ ____________________________________________________ _________ _______________________________________ _______ ______________________________________________ ____________________________________________
     62     _______________________ __________________________________________ __________________________________________________________ ____ ________________________________________ _______ _________________________________________ ____________________________________________
     63     ____________________________________
     64         _ ________________________________________ ______________________________________________ ___ _________________________________________ ___________________________________________ _______ _______________________________________
     65         _____________________ __________________________________________ ___________________ ________ ___________________________________________ _______ _______________________________________________________ _______ _______________________________________ ________ ____________________________________________________ _________ _______________________________________ ________ __________________________________________________________________ _______ ____________________________________________
     66         ____ ________________________________________ ________ _________________________________________ ___________________________________________ _____________________________________________ _______________________________________
     67     _____________________________________
     68 _____________________________________
     69 __________________ ______________ __________________________________________ ____________________________________________
     70 ____________________________________
     71     _____________________ __________________________________________ ___________________ _______ ___________________________________________ _____________________________________________ _______________________________________ _______ ____________________________________________________ ___ _______________________________________ _______ ______________________________________________ ____________________________________________
     72     ____________________________________
     73         ____________________ __________ ___________________________________________ _________ _______________________________________
     74         ______________________ __________________________________________ __________ ____________________________________________
     75         ____________________________________
     76             ___________ ________________________________________ _______ _________________________________________ _____________________________________________________ __________ ___________________________________________________ _ ________________________________________ _______ _________________________________________ _______________________________________
     77             __________ ________________________________________________________ _ ________________________________________ _______ _________________________________________ _______________________________________
     78         _____________________________________
     79     _____________________________________
     80 _____________________________________
     81 ____________________ _______________ __________________________________________ ____________________ _ ______________________________________ ____________________ __ ____________________________________________
     82 ____________________________________
     83     ____________________ __________ ___________________________________________ _____________________________________________ _______________________________________
     84     ______________________ __________________________________________ __ ____________________________________________
     85     ____________________________________
     86         _______________________ __________________________________________ __ __________________________________________________ _____________________________________________ ____________________________________________
     87         __________ ___________________________________________ __________ ________________________________________________ _ _________________________________________________ ____________ _______________________________________
     88         _ ___________________________________________ _ ________________________________________________ _ _________________________________________________ ____________ _______________________________________
     89         __ ______________________________________________________ _____________________________________________ _______________________________________
     90     _____________________________________
     91     ________________________ __________ _________________________________________________ ____________ _______________________________________
     92 _____________________________________
     93 ___________________ _________________________ __________________________________________ ____________________________________________
     94 ____________________________________
     95     __________________________ __________________________________________ ___________________________ ______________________________________ ____________________________ ______________________________________ _____________________________ ____________________________________________ _______________________________________
     96     __________________________ __________________________________________ ______________________________ ______________________________________ _______________________________ ______________________________________ ________________________________ ____________________________________________ _______________________________________
     97     _________________________________ __________________________________________ ___________________________________ ______________________________________ __________________________________________________ _________ ____________________________________________ _______________________________________
     98     ______ __________________________________________ ____________________________________________ _______________________________________
     99     ______________ __________________________________________ ____________________________________________ _______________________________________
    100     _____________________ __________________________________________ ___________________ _______ ___________________________________________ _____________________________________________ _______________________________________ _______ ____________________________________________________ ___ _______________________________________ _______ ______________________________________________ ____________________________________________
    101         _____ ___________________________________________ __________________________________________ _____ ________________________________________________ _______________ __________________________________________ _ ________________________________________ _______ _________________________________________ ______________________________________ ___________ ________________________________________ _______ _________________________________________ ___________________________________________________ _______________________________________________ ________________________________________________ _______________________________________________ ____________________________________________ _________________________________________________ ____________ ____________________________________________ _________________________________________________ ____________ _______________________________________
    102     __________________________________ __________________________________________ ___________________________________ ______________________________________ _____ ____________________________________________ _______________________________________
    103     ________________________ _________________________________________________________ _______________________________________
    104 _____________________________________
    code
     1 #include<cstdio>
     2 #define ____________ 100000007
     3 #define _____________ 5000000
     4 #define _____________________ for
     5 #define ______________________ while
     6 #define _______________________ if
     7 #define ________________________ return
     8 #define _________________________ main
     9 #define __________________________ freopen
    10 #define ___________________________ "hao.in"
    11 #define ____________________________ "r"
    12 #define _____________________________ stdin
    13 #define ______________________________ "hao.out"
    14 #define _______________________________ "w"
    15 #define ________________________________ stdout
    16 #define _________________________________ scanf
    17 #define __________________________________ printf
    18 #define ___________________________________ "%d"
    19 #define ____________________________________ {
    20 #define _____________________________________ }
    21 #define ______________________________________ ,
    22 #define _______________________________________ ;
    23 #define ________________________________________ [
    24 #define _________________________________________ ]
    25 #define __________________________________________ (
    26 #define ___________________________________________ =
    27 #define ____________________________________________ )
    28 #define _____________________________________________ 1
    29 #define ______________________________________________ ++
    30 #define _______________________________________________ 2
    31 #define ________________________________________________ *
    32 #define _________________________________________________ %
    33 #define __________________________________________________ &
    34 #define ___________________________________________________ /
    35 #define ____________________________________________________ <=
    36 #define _____________________________________________________ +=
    37 #define ______________________________________________________ >>=
    38 #define _______________________________________________________ +
    39 #define ________________________________________________________ /=
    40 #define _________________________________________________________ 0
    41 #define __________________________________________________________ !
    42 #define ___________________________________________________________ long
    43 #define ____________________________________________________________ typedef
    44 #define _____________________________________________________________ int
    45 #define ______________________________________________________________ void
    46 #define _______________________________________________________________ using
    47 #define ________________________________________________________________ namespace
    48 #define _________________________________________________________________ std
    49 #define __________________________________________________________________ +=
    50 _______________________________________________________________ ________________________________________________________________ _________________________________________________________________ _______________________________________ ____________________________________________________________ ___________________________________________________________ ___________________________________________________________ ____________________ _______________________________________ ____________________________________________________________ _____________________________________________________________ ___________________ _______________________________________ ____________________________________________________________ ______________________________________________________________ __________________ _______________________________________ ___________________ _________ ______________________________________ ___ _______________________________________ ___________________ ____ ________________________________________ _____________ _________________________________________ _______________________________________ ___________________ _ ________________________________________ _____________ _________________________________________ _______________________________________ ___________________ ___________ ________________________________________ _____________ _________________________________________ _______________________________________ ____________________ _____ ___________________________________________ _____________________________________________ _______________________________________ __________________ ______ __________________________________________ ____________________________________________ ____________________________________ _____________________ __________________________________________ ___________________ _______ ___________________________________________ _______________________________________________ _______________________________________ _______ ____________________________________________________ _________ _______________________________________ _______ ______________________________________________ ____________________________________________ _______________________ __________________________________________ __________________________________________________________ ____ ________________________________________ _______ _________________________________________ ____________________________________________ ____________________________________ _ ________________________________________ ______________________________________________ ___ _________________________________________ ___________________________________________ _______ _______________________________________ _____________________ __________________________________________ ___________________ ________ ___________________________________________ _______ _______________________________________________________ _______ _______________________________________ ________ ____________________________________________________ _________ _______________________________________ ________ __________________________________________________________________ _______ ____________________________________________ ____ ________________________________________ ________ _________________________________________ ___________________________________________ _____________________________________________ _______________________________________ _____________________________________ _____________________________________ __________________ ______________ __________________________________________ ____________________________________________ ____________________________________ _____________________ __________________________________________ ___________________ _______ ___________________________________________ _____________________________________________ _______________________________________ _______ ____________________________________________________ ___ _______________________________________ _______ ______________________________________________ ____________________________________________ ____________________________________ ____________________ __________ ___________________________________________ _________ _______________________________________ ______________________ __________________________________________ __________ ____________________________________________ ____________________________________ ___________ ________________________________________ _______ _________________________________________ _____________________________________________________ __________ ___________________________________________________ _ ________________________________________ _______ _________________________________________ _______________________________________ __________ ________________________________________________________ _ ________________________________________ _______ _________________________________________ _______________________________________ _____________________________________ _____________________________________ _____________________________________ ____________________ _______________ __________________________________________ ____________________ _ ______________________________________ ____________________ __ ____________________________________________ ____________________________________ ____________________ __________ ___________________________________________ _____________________________________________ _______________________________________ ______________________ __________________________________________ __ ____________________________________________ ____________________________________ _______________________ __________________________________________ __ __________________________________________________ _____________________________________________ ____________________________________________ __________ ___________________________________________ __________ ________________________________________________ _ _________________________________________________ ____________ _______________________________________ _ ___________________________________________ _ ________________________________________________ _ _________________________________________________ ____________ _______________________________________ __ ______________________________________________________ _____________________________________________ _______________________________________ _____________________________________ ________________________ __________ _________________________________________________ ____________ _______________________________________ _____________________________________ ___________________ _________________________ __________________________________________ ____________________________________________ ____________________________________ __________________________ __________________________________________ ___________________________ ______________________________________ ____________________________ ______________________________________ _____________________________ ____________________________________________ _______________________________________ __________________________ __________________________________________ ______________________________ ______________________________________ _______________________________ ______________________________________ ________________________________ ____________________________________________ _______________________________________ _________________________________ __________________________________________ ___________________________________ ______________________________________ __________________________________________________ _________ ____________________________________________ _______________________________________ ______ __________________________________________ ____________________________________________ _______________________________________ ______________ __________________________________________ ____________________________________________ _______________________________________ _____________________ __________________________________________ ___________________ _______ ___________________________________________ _____________________________________________ _______________________________________ _______ ____________________________________________________ ___ _______________________________________ _______ ______________________________________________ ____________________________________________ _____ ___________________________________________ __________________________________________ _____ ________________________________________________ _______________ __________________________________________ _ ________________________________________ _______ _________________________________________ ______________________________________ ___________ ________________________________________ _______ _________________________________________ ___________________________________________________ _______________________________________________ ________________________________________________ _______________________________________________ ____________________________________________ _________________________________________________ ____________ ____________________________________________ _________________________________________________ ____________ _______________________________________ __________________________________ __________________________________________ ___________________________________ ______________________________________ _____ ____________________________________________ _______________________________________ ________________________ _________________________________________________________ _______________________________________ _____________________________________
    code2

    (以上代码纯属本人因为上午电脑蓝屏重启导致代码+2K注释丢失后为报复社会而写,没有可读性,请直接忽略).

    正常代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 #define MAX 5000000
     5 #define MOD 100000007
     6 typedef long long ll;
     7 int n,tot;
     8 bool flag[MAX];
     9 int prime[MAX];
    10 int c[MAX];
    11 ll ans=1;
    12 void prepare()
    13 {
    14     for(int i=2;i<=n;i++)
    15     if(!flag[i])
    16     {
    17         prime[++tot]=i;
    18         for(int j=i+i;j<=n;j+=i)
    19         flag[j]=1;
    20     }
    21 }
    22 void jie()
    23 {
    24     for(int i=1;i<=tot;i++)
    25     {
    26         ll tmp=n;
    27         while(tmp)
    28         {
    29             c[i]+=tmp/prime[i];
    30             tmp/=prime[i];
    31         }
    32     }
    33 }
    34 ll quick_power(ll x,ll y)
    35 {
    36     ll tmp=1;
    37     while(y)
    38     {
    39         if(y&1)
    40         tmp=tmp*x%MOD;
    41         x=x*x%MOD;
    42         y>>=1;
    43     }
    44     return tmp%MOD;
    45 }
    46 int main()
    47 {
    48     #ifndef LOCAL
    49         freopen("hao.in","r",stdin);
    50         freopen("hao.out","w",stdout);
    51     #endif
    52     scanf("%d",&n);
    53     prepare();
    54     jie();
    55     for(int i=1;i<=tot;i++)
    56         ans=(ans*quick_power(prime[i],c[i]/2*2)%MOD)%MOD;
    57     printf("%d",ans);
    58     return 0;
    59 }
    View Code

    唯一可做的题目...能有思路的题目23333

    (jian.cpp/c/pas)

    【问题描述】
    有$N$个数,随机选择一段区间,如果这段区间的所有数的平均值在$[l,r]$中则
    你比较厉害。求你比较厉害的概率。
    【输入格式】
    第一行有三个数$N,l,r$,含义如上描述。
    接下来一行有$N$个数代表每一个数的值。
    【输出格式】
    输出一行一个分数 $frac ab$
    代表答案,其中$a$,$b$互质。如果答案为整数则直接输出该
    整数即可。
    【样例输入 1】
    4 2 3
    3 1 2 4
    【样例输出 1】
    7/10
    【样例输入 2】
    4 1 4
    3 1 2 4
    【样例输出 2】
    1
    【样例解释】
    塔外面有棵树。
    【数据规模与约定】
    3 4 。
    60%的数据,$1 leq N leq 10^5$ 。
    对于100%的数据,$1 leq $N$ leq 5 imes 10^5 ,0 < l leq r leq 100$。

    思路分析:树状数组维护前缀和,求区间$[1,l)$与$[1,r]$的和,然后作差求平均值。

    ????大概是这样???

    貌似暴力30分?

    时间复杂度:$O(nlog n)$.

    代码(完全看不懂,不要问我):

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    #ifdef unix
    #define LL "%lld"
    #else
    #define LL "%I64d"
    #endif
    
    #define lb(x) ((x)&(-(x)))
    
    const int maxn=500010;
    
    int n,l,r,y[maxn],z[maxn],w[maxn],x[maxn];
    
    bool cmp(int a,int b)
    {
        return z[a]>z[b];
    }
    
    void insert(int p)
    {
        for (;p<=n;p+=lb(p))
            w[p]++;
    }
    
    int query(int p)
    {
        int ans=0;
        for (;p;p-=lb(p))
            ans+=w[p];
        return ans;
    }
    
    long long solve()
    {
        for (int a=1;a<=n;a++)
            z[a]+=z[a-1];
        for (int a=n+1;a>=2;a--)
            z[a]=z[a-1];
        z[1]=0;
        n++;
        for (int a=1;a<=n;a++)
            x[a]=a;
        sort(x+1,x+n+1,cmp);
        memset(w,0,sizeof(w));
        long long ans=0;
        for (int a=1;a<=n;)
        {
            int b=a;
            while (b<=n && z[x[b]]==z[x[a]])
                b++;
            for (int c=a;c<b;c++)
                ans+=query(x[c]-1);
            for (int c=a;c<b;c++)
                insert(x[c]);
            a=b;
        }
        n--;
        return ans;
    }
    
    long long gcd(long long a,long long b)
    {
        if (!b) return a;
        else return gcd(b,a%b);
    }
    
    int main()
    {
        freopen("jian.in","r",stdin);
        freopen("jian.out","w",stdout);
    
        scanf("%d%d%d",&n,&l,&r);
        for (int a=1;a<=n;a++)
            scanf("%d",&y[a]);
        for (int a=1;a<=n;a++)
            z[a]=y[a]-l;
        long long ans=solve();
        for (int a=1;a<=n;a++)
            z[a]=r-y[a];
        ans+=solve();
        long long up=ans;
        long long down=(long long)n*(n+1)/2;
        up=down-up;
        long long g=gcd(up,down);
        up/=g;
        down/=g;
        if (up==0) printf("0
    ");
        else
        {
            if (down==1) printf("1
    ");
            else printf(LL "/" LL "
    ",up,down);
        }
    
        return 0;
    }
    View Code

    (dan.cpp/c/pas)

    【问题描述】
    $m imes m$的方阵上有$n$棵葱,你要修一些栅栏把它们围起来。一个栅栏是一段
    沿着网格建造的封闭图形(即要围成一圈) 。各个栅栏之间应该不相交、不重叠
    且互相不包含。如果你最多修$k$个栅栏,那么所有栅栏的长度之和最小是多少?
    【输入格式】
    第一行三个整数$m,k,n$。
    接下来$n$行每行两个整数$x,y$代表某棵葱的位置。
    【输出格式】
    一行一个整数代表答案。
    【样例输入 1】
    6 1 4
    1 3
    4 2
    4 4
    6 4
    【样例输出 1】
    18
    【样例输入 2】
    6 2 4
    1 3
    4 2
    4 4
    6 4
    【样例输出 2】
    16
    【样例解释】
    你猜树上有啥。
    【数据规模与约定】
    对于10%的数据,$k=1$。
    对于30%的数据,$kleq 2$。
    对于60%的数据,$nleq 10$。
    对于100%的数据,$1 leq k leq n leq 16,m leq 1000$。

    思路分析:这道题可做性比上一题强。暴力搜索能拿到30分左右。

    然而要想AC就需要用一个很神奇的动态规划。

    暴力+卡时最高95分。

    代码:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<ctime>
     5 #define maxn 1001
     6 #define minn -1
     7 using namespace std;
     8 int m,k,n,ans=0x7fffffff,p;
     9 int g[20][20];
    10 int cx[20],cy[20];
    11 int dhx[20],dhd[20],dlx[20],dld[20];
    12 int judge(int h[][20])
    13 {
    14       int s=0;
    15       for(int i=1;i<=k;i++)
    16              if(dhd[i]!=-1)
    17                 s+=(dhd[i]-dhx[i]+dld[i]-dlx[i]+2)*2;    
    18       return s; 
    19 }
    20 void change(int x,int d)
    21 {
    22     dhx[d]=min(dhx[d],cx[x]);
    23     dhd[d]=max(dhd[d],cx[x]);
    24     dlx[d]=min(dlx[d],cy[x]);
    25     dld[d]=max(dld[d],cy[x]);
    26  } 
    27 void dfs(int x,int a[][20])
    28 {
    29     if(clock()>1950)
    30     {
    31         printf("%d",ans);
    32         fclose(stdin);fclose(stdout);
    33         exit(0);
    34     }
    35     p=judge(a); 
    36     if(p>=ans) return;
    37     if(x==n+1)
    38      {
    39         if(p<ans) ans=p;
    40         return; 
    41      }
    42     for(int i=1;i<=k;i++)
    43      {
    44          a[i][++a[i][0]]=x;
    45          int khx=dhx[i],khd=dhd[i],klx=dlx[i],kld=dld[i];
    46         change(x,i);
    47         dfs(x+1,a);
    48         a[i][a[i][0]--]=0;
    49         dhx[i]=khx;dhd[i]=khd;dlx[i]=klx;dld[i]=kld;
    50      }
    51 } 
    52 int main()
    53 {
    54     freopen("dan.in","r",stdin);
    55     freopen("dan.out","w",stdout);
    56     scanf("%d%d%d",&m,&k,&n);
    57         for(int i=1;i<=k;i++)
    58     {
    59         dhx[i]=dlx[i]=1001;
    60         dhd[i]=dld[i]=-1;
    61     }
    62     for(int i=1;i<=n;i++)
    63     scanf("%d%d",&cx[i],&cy[i]);
    64     dfs(1,g);
    65     printf("%d",ans);
    66 } 
    dfs+卡时95
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int m,k,n,x[20],y[20],f[17][1<<16],cost[1<<16],s[20],ans;
     7 void dfs(int now,int cnt,int res)
     8 {
     9     if (now==n)
    10     {
    11         if (res<ans) ans=res;
    12         return;
    13     }
    14     if (res+(k-cnt)*4>=ans) return;
    15     for (int a=1;a<=cnt;a++)
    16     {
    17         int pres=s[a];
    18         s[a]|=(1<<now);
    19         dfs(now+1,cnt,res-cost[pres]+cost[s[a]]);
    20         s[a]^=(1<<now);
    21     }
    22     if (cnt<k)
    23     {
    24         cnt++;
    25         s[cnt]=(1<<now);
    26         dfs(now+1,cnt,res+4);
    27     }
    28 }
    29 int main()
    30 {
    31     freopen("dan.in","r",stdin);
    32     freopen("dan.out","w",stdout);
    33     scanf("%d%d%d",&m,&k,&n);
    34     for (int a=0;a<n;a++)
    35         scanf("%d%d",&x[a],&y[a]);
    36     if (n<=14)
    37     {
    38         memset(f,0x3f,sizeof(f));
    39         for (int a=1;a<(1<<n);a++)
    40         {
    41             int sx=1001,mx=-1,sy=1001,my=-1;
    42             for (int b=0;b<n;b++)
    43                 if ((a>>b)&1)
    44                 {
    45                     if (x[b]<sx) sx=x[b];
    46                     if (x[b]>mx) mx=x[b];
    47                     if (y[b]<sy) sy=y[b];
    48                     if (y[b]>my) my=y[b];
    49                 }
    50             f[1][a]=(mx-sx+1)*2+(my-sy+1)*2;
    51         }
    52         for (int a=2;a<=k;a++)
    53             for (int b=0;b<(1<<n);b++)
    54                 for (int c=b-1;c;c=(c-1)&b)
    55                     f[a][b]=min(f[a][b],f[a-1][c]+f[1][b^c]);
    56         int ans=f[1][(1<<n)-1];
    57         for (int a=2;a<=k;a++)
    58             ans=min(ans,f[a][(1<<n)-1]);
    59         printf("%d
    ",ans);
    60     }
    61     else
    62     {
    63         ans=4000;
    64         for (int a=1;a<(1<<n);a++)
    65         {
    66             int sx=1001,mx=-1,sy=1001,my=-1;
    67             for (int b=0;b<n;b++)
    68                 if ((a>>b)&1)
    69                 {
    70                     if (x[b]<sx) sx=x[b];
    71                     if (x[b]>mx) mx=x[b];
    72                     if (y[b]<sy) sy=y[b];
    73                     if (y[b]>my) my=y[b];
    74                 }
    75             cost[a]=(mx-sx+1)*2+(my-sy+1)*2;
    76         }
    77         dfs(0,0,0);
    78         printf("%d
    ",ans);
    79     }
    80     return 0;
    81 }
    std

    End.

  • 相关阅读:
    HTML iframe, 获取iframe子页面中的元素(基于JavaScript)
    微信小程序中引入VR全景图
    git创建分支,提交代码详细流程(保姆级)
    Vue项目中使用Axios封装http请求
    node搭建本地服务器后端解决跨域问题
    ant design Upload组件上传文件类型
    使用nodejs连接mysql数据库实现增删改查 连接Node.js时报错“Cannot GET /”
    后端返回文件流,前端处理进行文件下载
    JS判断是否是数组的四种做法
    Js常用方法:JS字符串截取、数组截取等
  • 原文地址:https://www.cnblogs.com/TheRoadToAu/p/7091557.html
Copyright © 2011-2022 走看看