- 选数
- 2015-08-20 13:42:39 @
#include<stdio.h>
#include<iostream>
using namespace std;
const int N=50;
int n,k,a[N]={0},ans=0,cnt=0,sum=0;
bool check(int x){
for(int i=2;i*i<=x;i++)
if(x%i==0) return false;
return true;
}
void DFS(int t){
if(cnt==k){cout<<sum<<" sum \n";
if(check(sum)){ans++;}
return;
}
for(int i=t;i<=n;i++){
sum+=a[i];
cnt++;
DFS(i+1);
cnt--;
sum-=a[i];
}
return;
}
int main(){
int i;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
DFS(1);
printf("%d",ans);
while(1);
return 0;
}
3 条评论
-
TenderRun LV 10 @ 2015-09-16 21:13:17
记录信息
评测状态 Accepted
题目 P1128 选数
递交时间 2015-07-28 21:33:32
代码语言 C++
评测机 VijosEx
消耗时间 0 ms
消耗内存 280 KiB
评测时间 2015-07-28 21:33:36
评测结果
编译成功测试数据 #0: Accepted, time = 0 ms, mem = 276 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 280 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 276 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 280 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 280 KiB, score = 10
Accepted, time = 0 ms, mem = 280 KiB, score = 50
代码
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int a[25];
int n,k;
int ans=0;
bool is_prime(int a)
{
for(int i=2;i<=sqrt(a);i++)
if(a%i==0)return false;
return true;
}
void work(int now,int total,int where)
{
if(where>n+1)return;
if(now==k)
{
if(is_prime(total)) ans++;
return;
}
work(now+1,total+a[where],where+1);
work(now,total,where+1);
}
int main()
{ scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
work(0,0,1);
printf("%d",ans);
return 0;
} -
2015-09-12 08:25:13@
好奇怪的写法
-
2015-08-20 13:43:22@
while(1);评测的时候没有加,只对了一个点。。
- 1