Game(hdu5218)

Game

 
 Accepts: 138
 
 Submissions: 358
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 131072/131072 K (Java/Others)
Problem Description

BrotherK is very rich. He has a big company.html

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.ios

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

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

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 X_iXi​​ is chosen randomly from a given integer set SS. When there is only one employee left in the game, the game ends and the employee wins the award.ide

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

Input

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

Each test case begins with two integer N, MN,M, indicating the number of employees in BrotherK's company, and the size of BrotherK's integer set SS. Next line contains MM numbers, from a_1a1​​ to a_MaM​​, indicating the integer in SS.orm

TT is about 50htm

1~\le~N, M~\le~2001  N,M  200blog

1~\le~a_i~\le~10^91  ai​​  109​​

Output

For each test, print two lines.

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

The second line contains KK 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
思路:dp+約瑟夫環;
約瑟夫環的問題能夠用遞推來解決,ans[n] = (ans[n-1]+m)%n;那麼此次每次能夠有多個間隔,那麼dp[i][j]表示當執行第n-i+1次時下標爲j是否存在;具體過程看代碼
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<stack>
 9 #include<map>
10 #include<vector>
11 #include<algorithm>
12 using namespace std;
13 typedef long long LL;
14 bool flag[10000];
15 int dp[205][205];
16 int ask[1000];
17 int main(void)
18 {
19     int T;
20     scanf("%d",&T);
21     while(T--)
22     {
23         int n,m;
24         memset(flag,0,sizeof(flag));
25         scanf("%d %d",&n,&m);
26         int i,j;
27         memset(dp,-1,sizeof(dp));
28         for(i = 2; i < 205; i++)
29         {
30             for(j = 0; j < 205; j++)
31             {
32                 dp[i][j] = -1;
33             }
34         }
35         for(i = 0; i < m; i++)
36         {
37             scanf("%d",&ask[i]);
38         }
39         dp[1][0] = 1;
40         for(i = 2; i <= n; i++)
41             for(j = 0; j <= i; j++)
42                 if(dp[i-1][j]!=-1)
43                     for(int s = 0; s < m; s++)
44                         dp[i][(j+ask[s])%i] = 1;
45         int sum = 0;
46         int ac[1000];
47         for(i = 0; i < n; i++)
48         {
49             if(dp[n][i]==1)
50             {
51                 ac[sum++] = i+1;
52             }
53         }
54         printf("%d\n",sum);
55         printf("%d",ac[0]);
56         for(i = 1; i < sum; i++)
57             printf(" %d",ac[i]);
58         printf("\n");
59     }
60     return 0;
61 }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息