-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocialPairings.java
273 lines (224 loc) · 7.16 KB
/
socialPairings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Social Pairings (prob3)
The Problem
John is the CEO of a company. He recently organized a social event for two branches of his
company. In order to encourage the attendees to socialize with people from the other branch,
John gave each employee a required task at the gathering: have a conversation with exactly 2
people from the other branch. He emphasized exactly 2 people, since if it was more than 2
people, the conversations may not be as meaningful. Thankfully, each of the branches had the
same number of employees, otherwise it would be impossible for his requirement to be satisfied.
As John sits and watches his employees socialize, being a curious man, he wonders how many
different possible ways there are of the employees conversing given his requirement. John
assigns this question to you, his trusted technical advisor. Since John is the CEO, you do not
want to disappoint him. Can you figure out how many ways there are and satisfy John's
curiosity?
Input
The first line of input will be a single integer t, representing the number of test cases. Then, t
lines follow, each containing a single integer n, 1<=n<=2000, the number of employees at each
branch of the company
Output
For each test case, output the number of possible distinct ways that the two branches can
socialize under the restriction that each employee must socialize with exactly 2 employees from
the other branch. Since this number may be very large, output it modulo 10^8+7.
Sample Input
5
1
2
3
4
1932
Sample Output
0
1
6
90
87032144
*/
import java.lang.Math.*;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Set;
import java.util.Iterator;
import java.util.Collections;
import java.util.ArrayList;
public class socialPairings
{
public static DecimalFormat df;
public static void main(String[] args)
{
TreeMap<String, Integer> person_count = new TreeMap<String, Integer>();
TreeSet<String> choice_set = new TreeSet<String>();
int choices = 4;
String zeros = "";
for(int i=0; i<choices; i++)
zeros += "0";
df = new DecimalFormat(zeros);
int num_of_possibilities = (int)Math.pow(2, choices);
for(int i = 0; i< num_of_possibilities; i++)
{
//generate choice map, to count how many times each person has been chosen
if(Integer.bitCount(i) == 1)
{
person_count.put(df.format(Integer.parseInt(Integer.toBinaryString(i))), 0);
}
//generate possible choices (only 2 persons per choice)
if(Integer.bitCount(i) == 2)
{
String binary_string = Integer.toBinaryString(i);
choice_set.add(""+df.format(Integer.parseInt(binary_string)));
}
}
int count_how_many_choices = 1;
//pick one then remove it from the list
for(int i=0; i<choices; i++)
{
System.out.println(choice_set);
//make a choice
count_how_many_choices *= choice_set.size();
String choice = validChoice(person_count, choice_set);
if(choice_set.size() > 1)
choice_set.remove(choice);
System.out.println("Choice: "+choice);
//count how many times that person has been chosen
personCount(person_count, choice);
removePersonFromChoices(person_count, choice_set);
System.out.println(person_count);
}
System.out.println("Number of choices "+count_how_many_choices);
}
public static String validChoice(TreeMap<String, Integer> person_count, TreeSet<String> choice_set)
{
//a valid choice returns a choice that has zero persons chosen
//return last entry will return zero
if(person_count.size() > 0)
{
//if 2 unique 0's
/*
if((!getFirstKeyByValue(0, person_count).equals("") && !getLastKeyByValue(0, person_count).equals("")) && !getFirstKeyByValue(0, person_count).equals(getLastKeyByValue(0, person_count)))
{
String index1 = getFirstKeyByValue(0, person_count);
String index2 = getLastKeyByValue(0, person_count);
return selectChoice(index1.indexOf('1'), index2.indexOf('1'), choice_set);
}
else
*/
if(!getFirstKeyByValue(0, person_count).equals("")) //1 zero
{
String index1 = getFirstKeyByValue(0, person_count);
return selectChoice(index1.indexOf('1'), choice_set);
}
else
{
String index1 = getFirstKeyByValue(1, person_count);
return selectChoice(index1.indexOf('1'), choice_set);
}
}
else
return "";
}
public static void personCount(TreeMap<String, Integer> person_count, String choice)
{
while(choice.indexOf('1') != -1)
{
int index = choice.indexOf('1');
StringBuilder replace1 = new StringBuilder(choice);
replace1.setCharAt(index, '0');
choice = replace1.toString();
String zero_string = ""+df.format(0);
StringBuilder makeKey = new StringBuilder(zero_string);
makeKey.setCharAt(index, '1');
String person_key = makeKey.toString();
int new_count = person_count.get(person_key);
new_count++;
person_count.put(person_key, new_count);
}
}
public static void removePersonFromChoices(TreeMap<String, Integer> person_count, TreeSet<String> choice_set)
{
//remove person from choice if they have been accounted for twice
Set set = person_count.entrySet();
Iterator <Map.Entry> entry_it = set.iterator();
while(entry_it.hasNext())
{
Map.Entry me = entry_it.next();
if(me.getValue() == 2)
{
//remove from choice set
removeChoice(""+me.getKey(), choice_set);
}
}
}
public static void removeChoice(String person, TreeSet<String> choice_set)
{
//remove the choice from choice set if it has a 1 where the persons index of 1 is
int indexOf1 = person.indexOf("1");
Iterator<String> choice_set_it = choice_set.iterator();
while(choice_set_it.hasNext())
{
String choice = choice_set_it.next();
if(choice.charAt(indexOf1) == '1')
{
//remove from iterator and it will remove from set
choice_set_it.remove();
}
}
}
public static String selectChoice(int index, int index2, TreeSet<String> choice_set)
{
Iterator<String> choice_set_it = choice_set.iterator();
String ret ="";
while(choice_set_it.hasNext())
{
String choice = choice_set_it.next();
if(choice.charAt(index) == '1' && choice.charAt(index2) == '1')
{
//remove from iterator and it will remove from set
ret = choice;
}
}
return ret;
}
public static String selectChoice(int index, TreeSet<String> choice_set)
{
Iterator<String> choice_set_it = choice_set.iterator();
String ret ="";
while(choice_set_it.hasNext())
{
String choice = choice_set_it.next();
if(choice.charAt(index) == '1')
{
//remove from iterator and it will remove from set
ret = choice;
}
}
return ret;
}
public static String getFirstKeyByValue(int value, TreeMap<String, Integer> person_count)
{
Set set = person_count.entrySet();
Iterator <Map.Entry> entry_it = set.iterator();
while(entry_it.hasNext())
{
Map.Entry me = entry_it.next();
if(me.getValue() == value)
{
return ""+me.getKey();
}
}
return "";
}
public static String getLastKeyByValue(int value, TreeMap<String, Integer> person_count)
{
ArrayList<String> keys = new ArrayList<String>(person_count.keySet());
for(int i=keys.size()-1; i>=0; i--)
{
if(person_count.get(keys.get(i)) == value)
{
return keys.get(i);
}
}
return "";
}
}