zoukankan      html  css  js  c++  java
  • luogu cogs 选数

    题目描述

    已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n)。从 n 个整数中任选 k 个整数相加,可分别得到一系列的和。例如当 n=4,k=3,4 个整数分别为 3,7,12,19 时,可得全部的组合与它们的和为:

    3+7+12=22

    3+7+19=29

    7+12+19=38

    3+12+19=34。

    现在,要求你计算出和为素数共有多少种。

    例如上例,只有一种的和为素数:3+7+19=29)。

    输入输出格式

    输入格式:

    键盘输入,格式为:

    n , k (1<=n<=20,k<n)

    x1,x2,…,xn (1<=xi<=5000000)

    输出格式:

    屏幕输出,格式为:

    一个整数(满足条件的种数)。

    输入输出样例

    输入样例#1:
    4 3
    3 7 12 19
    
    输出样例#1:
    1
    
     1 #include <iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstdio>
     7 
     8 using namespace std;
     9 int aa[30],a[30],d=0,n,k,nn=0;
    10 
    11 inline bool s(int x) 
    12 {
    13     if(x==1)return false;
    14     for(int c=2; c*c<=x; c++)
    15         if(x%c==0)return false;
    16     return true;
    17 }
    18 
    19 inline int read ()
    20 {
    21     char c=getchar();
    22     int x=0;
    23     while(c<'0'||c>'9')c=getchar();
    24     while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    25     return x;
    26 }
    27 
    28 inline int dfs(int d, int pre) 
    29 {
    30     int i,ss=0;
    31     if(d==k) //sou dao di k ge shu
    32     {
    33         for(i=0; i<k; i++)
    34             ss+=aa[a[i]];
    35         if(s(ss))
    36             nn++;
    37         return nn;
    38     }
    39     for(i=pre; i<n; i++) 
    40     {
    41         a[d]=i;
    42         dfs(d+1,i+1);
    43     }
    44 }
    45 
    46 int main()
    47 {
    48     freopen("choose.in","r",stdin);
    49     freopen("choose.out","w",stdout);
    50     n=read();
    51     k=read();
    52     for(int c=0; c<n; c++)
    53         aa[c]=read();
    54     dfs(0,0);
    55     printf("%d",nn);
    56     return 0;
    57 }
  • 相关阅读:
    极速地将git项目部署到SAE的svn服务器上
    深入了解Javascript模块化编程
    自己实现的一款在线Javascript正则表达式测试器——JRE-Parser
    Javascript中的一种深复制实现
    如何循序渐进地学习Javascript
    handsontable实例----来源github
    HandsontableWithVue (一) 导入官方的汉化包
    Handsontable 入坑的开始
    开始前端的生活
    c3p0数据源的第一次尝试
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7055207.html
Copyright © 2011-2022 走看看