回答編集履歴
1
追加。
test
CHANGED
@@ -5,3 +5,195 @@
|
|
5
5
|
使える数字に’0’があった場合、頭が’0’のものは’0’だけである。(解の1つ)
|
6
6
|
|
7
7
|
後は桁数になる数字の組み合わせをカウントして、その組み合わせ数が前述の’0’が選択できる数字にある場合は+1して偶数か奇数かを判断し、それを算出する。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
15桁までという事を考えてlong long intで処理してみる。
|
12
|
+
|
13
|
+
```C++
|
14
|
+
|
15
|
+
#include <iostream>
|
16
|
+
|
17
|
+
#include <string>
|
18
|
+
|
19
|
+
#include <vector>
|
20
|
+
|
21
|
+
unsigned char used_number[10]; // 使用数字 0=not used,1=used
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
long long int check_number(int digit)
|
26
|
+
|
27
|
+
{
|
28
|
+
|
29
|
+
long long int work;
|
30
|
+
|
31
|
+
if( digit == 1 )
|
32
|
+
|
33
|
+
{
|
34
|
+
|
35
|
+
for(int i = 0,work = 0LL;i <10;i++)
|
36
|
+
|
37
|
+
if(used_number[i] != 0)
|
38
|
+
|
39
|
+
work++;
|
40
|
+
|
41
|
+
return work;
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
for(int i = 0;i<10;i++)
|
46
|
+
|
47
|
+
work += check_number(digit-1);
|
48
|
+
|
49
|
+
if(used_number[0] == 1) // 頭が’0’場合、その数を引く
|
50
|
+
|
51
|
+
work -= number_of_count;
|
52
|
+
|
53
|
+
return work;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
long long int calc_number(int digit,long long int current_pos,long long int result_pos)
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
long long work;
|
62
|
+
|
63
|
+
if( digit == 1)
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
for(int i = 0,work = 0LL;i<10;i++ )
|
68
|
+
|
69
|
+
{
|
70
|
+
|
71
|
+
if(used_number[i] != 0 )
|
72
|
+
|
73
|
+
{
|
74
|
+
|
75
|
+
if(work == result_pos )
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
cout << used_number[i] ;
|
80
|
+
|
81
|
+
return;
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
return work;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
for(int i = 0;i<10;i++)
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
current_pos += calc_number(digit-1,current_pos,result_pos);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
if(used_number[0] == 1) // 頭が’0’場合、その数を引く
|
102
|
+
|
103
|
+
current_pos -= number_of_count;
|
104
|
+
|
105
|
+
return current_pos;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
int main(void) {
|
112
|
+
|
113
|
+
int digit;
|
114
|
+
|
115
|
+
std::string number;
|
116
|
+
|
117
|
+
long long int work;
|
118
|
+
|
119
|
+
int number_count;
|
120
|
+
|
121
|
+
long long int count_of_result=0LL;
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
std::cin >> digit >> number;
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
if((digit <= 0) || ( digit >15))
|
130
|
+
|
131
|
+
{
|
132
|
+
|
133
|
+
cout << "Illegal number" << endl;
|
134
|
+
|
135
|
+
return -1;
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
// 重複確認
|
140
|
+
|
141
|
+
for(int i = 0;i<10;i++)
|
142
|
+
|
143
|
+
used_number[i]= 0;
|
144
|
+
|
145
|
+
for(int i = 0,number_count =0;i<strlen(number);i++)
|
146
|
+
|
147
|
+
{
|
148
|
+
|
149
|
+
if((number[i] >= '0')&&(number[i] <='9'))
|
150
|
+
|
151
|
+
{
|
152
|
+
|
153
|
+
if( used_number[(number[i]-'0')] == 0 )
|
154
|
+
|
155
|
+
{
|
156
|
+
|
157
|
+
number_of_count++;
|
158
|
+
|
159
|
+
used_number[(number[i]-'0')]=1;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
else {
|
166
|
+
|
167
|
+
cout << "Illegal number" << endl;
|
168
|
+
|
169
|
+
return -1;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
// 解の数を算出
|
176
|
+
|
177
|
+
work = check_number(ditit);
|
178
|
+
|
179
|
+
//
|
180
|
+
|
181
|
+
if( work & 1 ) //解が奇数
|
182
|
+
|
183
|
+
{ // 答えは2つ
|
184
|
+
|
185
|
+
cout << calc_number(work/2) << "," << calc_number((work/2)+1) << endl;
|
186
|
+
|
187
|
+
} else
|
188
|
+
|
189
|
+
{ // 答えは1つ
|
190
|
+
|
191
|
+
cout << calc_number(work/2) << endl;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
机上なのでバグあるだろうなぁ。
|