赛码"BestCoder"杯中国大学生程序设计冠军赛1005——DP——Game

Problem Description

BrotherK is very rich. He has a big company.

One day, BrotherK wants to give an award to one of the employees in the company. It's so hard to make a choise that BrotherK decides to play a game to determine the lucky dog.

At the beginning of the game, all N employees form a circle. Then, they receive t-shirts with numbers 1 through N in clockwise order along the circle.

The game consists of many turns. In the ith turn, BrotherK starts by standing in front of a employee and announces a number Xi. He will move to the (Xi+1)th-next-employee in clockwise order, and the Xith-next-employees will be removed from the circle, then the (i+1)th turn starts.

Attention that the 1th-next-employee of a employee is itself, and BrotherK stands in front of the employee with number 1 at first. In the ith turn, The value of Xi is chosen randomly from a given integer set S. When there is only one employee left in the game, the game ends and the employee wins the award.

You are given the int N and the set of integers, BrotherK wants to know which employees are possible to win.

Input

The first line contains a single integer T, indicating the number of test cases.

Each test case begins with two integer N,M, indicating the number of employees in BrotherK's company, and the size of BrotherK's integer set S. Next line contains M numbers, from a1 to aM, indicating the integer in S.

T is about 50

1  N,M  200

1  ai  109

Output

For each test, print two lines.

The first line contains a integer K, indicating how many people are possible to win.

The second line contains K number, indicating the number in T-shirt who can win, in ascending order.

Sample Input
2
3 1
1
3 2
1 2
Sample Output
1
3
3
1 2 3
大意:类似约瑟夫环,不过他报的数是在一个set里面的,问你最后有几种情况,并且输出可能的情况
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[210][210],s[210];
int main()
{
    int T,n,m;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(dp,0,sizeof(dp));
        dp[1][0] = 1;
        for(int i = 1; i <= m ; i++)
            scanf("%d",&s[i]);
        for(int i = 2; i <= n ; i++){
            for(int j = 0; j <= n; j++){
                if(dp[i-1][j]){
                    for(int k = 1; k <= m ;k++){
                        dp[i][(j+s[k])%i] = 1;
                    }
                }
            }
        }
        int cnt = 0;
        for(int i = 0; i < n ; i++){
            if(dp[n][i])
                cnt++;
        }
        int x = 0;
        printf("%d
",cnt);
        for(int i = 0 ; i < n ; i++){
            if(dp[n][i]){
                x++;
                if( x < cnt)
                printf("%d ",i+1);
                else printf("%d
",i+1);
            }
        }
      }
    return 0;
}

  

 
原文地址:https://www.cnblogs.com/zero-begin/p/4477513.html