2015
04-13

# How many people have ipad II

hh found more and more of his friends are having ipad IIs. One day when they get together, hh asked his five friends, "How many of you have ipad II now?"

"One!"
"Three!"
"Everyone!"
"Four!"
"Two!"

hh’s friends knew each other. They were clear about the "how many" question, while the answers are different, so there must be some people telling lies.

One of hh’s friends told him(hh):
number of people, who had ipad IIs, and lied, was no more than 1.
number of people, who didn’t have ipad IIs, and told the truth, was no more than 2.
at least one people have ipad II.

Given the information, hh realized there may be one or two people having ipad IIs.

Now hh asks N people the "how many" question. These N friends answer one by one. Some tell the truth, some lie. What hh knows is:
1.number of people, who have ipad IIs, and lie, is no more than A.
2.number of people, who don’t have ipad IIs, and tell the truth, is no more than B.
3.At least one people have ipad II.

How many ipad IIs do these N people have?

The input begins with an integer T(1<=T<=100).
The next T blocks each indicates a case.
The first line of each case contain a number N(1<=N<=16) then N positive integers follow, integers won’t be lager than N.
Then following two numbers A , B(0 <= A,B <= N).

The input begins with an integer T(1<=T<=100).
The next T blocks each indicates a case.
The first line of each case contain a number N(1<=N<=16) then N positive integers follow, integers won’t be lager than N.
Then following two numbers A , B(0 <= A,B <= N).

3
5
1 2 3 4 5
1 2

3
0 0 0
1 1

4
0 0 0 0
0 1

1 2
1
impossible

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#define INF 0x7f7f7f7f
using namespace std;

int num[1010];
vector<int> ans;
int main()
{
int t, n, x, A, B;
scanf("%d", &t);
while(t--)
{
memset(num, 0, sizeof(num));
ans.clear();
scanf("%d", &n);
for(int i = 0; i < n; ++ i)
{
scanf("%d",&x);
num[x]++;
}
scanf("%d%d", &A, &B);
for(int i = 1; i <= n; ++ i)
{
for(int j = 0; j <= A; ++ j)
{
int lie = n-num[i];
if(i-j >= 0 && lie-j >= 0 && (n-(i-j+lie) >= 0 && n-(i-j+lie) <= B))
{
ans.push_back(i);
break;
}
}
}
if((int)ans.size() == 0)
printf("impossible\n");
else
{
int len = ans.size();
for(int i = 0; i < len-1; ++ i)
printf("%d ", ans[i]);
printf("%d\n", ans[len-1]);
}
}
return 0;
}


1. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count