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
| #include <iostream> #include <queue> #define MAX 100000 using namespace std; int N, K; queue<int> q; bool visit[MAX+1]; int dist[MAX + 1]; int cnt[MAX + 1]; void BFS() { q.push(N); visit[N] = true; dist[N] = 0; cnt[N] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int next : {x - 1, x + 1, x * 2}) { if (0 <= next && next <= MAX) { if (!visit[next]) { visit[next] = true; dist[next] = dist[x] + 1; cnt[next] = cnt[x]; q.push(next); } else if (dist[next] == dist[x] + 1) { cnt[next] += cnt[x]; } } } } } int main() { cin >> N >> K; BFS(); cout << dist[K] << "\n" << cnt[K] << "\n"; return 0; }
|