-
Notifications
You must be signed in to change notification settings - Fork 34
/
1141-Number-Tranformation.cpp
96 lines (59 loc) · 1.2 KB
/
1141-Number-Tranformation.cpp
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
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <bits/stdc++.h>
using namespace std;
int x;
int y;
vector <int> pr;
int main()
{
int t;
int primes[100];
int temp;
memset(primes, 0, sizeof(primes));
for (int i = 2; i <= 1000; i++) {
if(primes[i] == 0) {
for (int j = 2 * i; j <= 1000; j = j + i) {
primes[j] = 1;
}
}
}
for (int i = 2; i <= 1000; i++) {
if(primes[i] == 0) {
pr.push_back(i);
}
}
cout << pr.size() << endl;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &x);
scanf("%d", &y);
int ans[2000];
for(int i = 0; i <= y; i++) {
ans[i] = INT_MAX;
}
ans[x] = 0;
int k;
for (int i = x + 1; i <= y; i++) {
for (int j = 0; j < pr.size(); j++) {
k = pr[j];
if(((i - k) % k == 0) and (i-k != k) and i - k >= x and ans[i-k] != INT_MAX) {
if(ans[i] > 1 + ans[i-k]) {
ans[i] = min(ans[i], 1 + ans[i-k]);
//cout << "k " << k << " " << ans[i-k] << endl;
}
}
}
//cout << i << " "<<ans[i] << endl;
}
if(ans[y] != INT_MAX) {
printf("Case %d: %d\n", cs, ans[y]);
}
else {
printf("Case %d: -1\n", cs);
}
}
}