AOJ1500 ID

ID | Aizu Online Judge

Introduction to Programming Introduction to Algorithms and Data Structures Library of Data Structures Library of Graph Algorithms Library of Computational Geometry Library of Dynamic Programming Library of Number Theory

‘?'に入るを全探索.’?‘の数が小さいので十分間に合う.stringstream最初使ってて,やっぱり遅い…となった.

Code

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
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int f(int x, bool f) {
  int ret = 0;
  if(f) x *= 2;
  while(x) {
      ret += (x % 10);
      x /= 10;
  }
  return ret;
}

vector<int> id, a;
int n, m;
int cnt = 0;

void dfs(int i, int res) {
  if(i == id.size()) {
      if(res % 10 == 0) {
          cnt++;
      }
      return;
  }

  rep(j, m) {
      if((n - id[i]) % 2 == 0) {
          dfs(i + 1, res + f(a[j], true));
      } else {
          dfs(i + 1, res + f(a[j], false));
      }
  }
}

int main() {
  cin >> n;

  string s;
  cin >> s;

  cin >> m;

  a.resize(m);
  rep(i, m) cin >> a[i];
  sort(a.begin(), a.end());

  int sum = 0;
  rep(i, n) {
      if(s[i] =='*') {
          id.push_back(i);
      } else {
          if((n - i) % 2 == 0) {
              sum += f(s[i] - '0', true);
          } else {
              sum += f(s[i] - '0' , false);
          }
      }
  }

  cnt = 0;
  dfs(0, sum);

  cout << cnt << endl;

  return 0;
}
Apr 19th, 2016