UVa 11730 - Number Transformation
/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1002;
const int qrt = 32;
bool mark[high] , visited[high];
int prm[high] , plen=0 , cost[high] , S , T;
void sieve()
{
int i , j;
mset(mark, false);
for(i=3; i<qrt; i+=2)
{
if(!mark[i])
{
for(j=i*i; j<high; j+=(2*i))
{
mark[j] = true;
}
}
}
prm[plen++] = 2;
for(i=3; i<high; i+=2)
{
if(!mark[i])
{
prm[plen++] = i;
}
}
//for(i=0; i<100; i++) cout << prm[i] << "; ";
}
int BFS(int s)
{
mset(visited, false);
mset(cost , 0);
int A , x , sum=0;
visited[s] = true;
cost[s] = 0;
if(s == T) return 0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
A = Q.front();
Q.pop();
for(int i=0; i<plen && prm[i] < A; i++)
{
x = prm[i];
if(!(A % x))
{
sum = A + x;
if(sum == T) return cost[A] + 1;
if(!visited[sum] && sum <= T)
{
visited[sum] = true;
cost[sum] = cost[A] + 1;
Q.push(sum);
}
}
}
}
return -1;
}
int main()
{
//freopen("out.txt", "w" , stdout);
fast;
sieve();
int tc = 0;
while(cin >> S >> T)
{
if(!S && !T) break;
int ans = BFS(S);
cout << "Case " << ++tc << ": ";
outn(ans);
}
return 0;
}
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 1002;
const int qrt = 32;
bool mark[high] , visited[high];
int prm[high] , plen=0 , cost[high] , S , T;
void sieve()
{
int i , j;
mset(mark, false);
for(i=3; i<qrt; i+=2)
{
if(!mark[i])
{
for(j=i*i; j<high; j+=(2*i))
{
mark[j] = true;
}
}
}
prm[plen++] = 2;
for(i=3; i<high; i+=2)
{
if(!mark[i])
{
prm[plen++] = i;
}
}
//for(i=0; i<100; i++) cout << prm[i] << "; ";
}
int BFS(int s)
{
mset(visited, false);
mset(cost , 0);
int A , x , sum=0;
visited[s] = true;
cost[s] = 0;
if(s == T) return 0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
A = Q.front();
Q.pop();
for(int i=0; i<plen && prm[i] < A; i++)
{
x = prm[i];
if(!(A % x))
{
sum = A + x;
if(sum == T) return cost[A] + 1;
if(!visited[sum] && sum <= T)
{
visited[sum] = true;
cost[sum] = cost[A] + 1;
Q.push(sum);
}
}
}
}
return -1;
}
int main()
{
//freopen("out.txt", "w" , stdout);
fast;
sieve();
int tc = 0;
while(cin >> S >> T)
{
if(!S && !T) break;
int ans = BFS(S);
cout << "Case " << ++tc << ": ";
outn(ans);
}
return 0;
}
Comments
Post a Comment