Pagini recente » Cod sursa (job #1244853) | Cod sursa (job #2938487) | Cod sursa (job #2932871) | Cod sursa (job #1240843) | Cod sursa (job #2382753)
#include <bits/stdc++.h>
#define maxN 16
#define maxCf (1 << maxN) + 2
#define maxP 14145
#define v first
#define c second
#define pii pair<int, int>
#define ll long long
using namespace std;
FILE *fin = freopen("vanatoare.in", "r", stdin);
FILE *fout = freopen("vanatoare.out", "w", stdout);
int n, t;
pii b[maxN + 1];
bool vis[maxCf];
struct State
{
int p, a, cost;
vector < int > sol;
};
State dp[maxCf];
int ExtEuclid(int a, int b, int &x, int &y)
{
int x0, y0;
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int z = a / b, c = a;
a = b;
b = c % b;
int d = ExtEuclid(a, b, x0, y0);
x = y0;
y = x0 - z * y0;
return d;
}
int MinVal(int bit, int cf)
{
if (cf == 0)
{
dp[cf | (1 << bit)].p = b[bit].v;
return b[bit].c;
}
pii A = pii{dp[cf].p, dp[cf].a},
B = b[bit];
if (B.c < A.c) swap(A, B);
int x, y, gcd, r;
gcd = ExtEuclid(A.v, B.v, x, y);
r = B.c - A.c;
if (r % gcd) return t + 1;
r /= gcd;
B.v /= gcd;
ll lcm = 1LL * B.v * A.v / gcd;
ll ret = (1LL * x * A.v * r + A.c) % lcm;
dp[cf | (1 << bit)].p = lcm;
if (ret > t) return t + 1;
return (int)ret;
}
void Update(int cf)
{
int prv = 0;
for (int prvCf = cf & (cf - 1); prvCf != 0; prvCf = cf & (prvCf - 1))
if (dp[cf].cost > dp[cf ^ prvCf].cost + dp[prvCf].cost)
{
dp[cf].cost = dp[cf ^ prvCf].cost + dp[prvCf].cost;
prv = prvCf;
}
dp[cf].sol.resize(dp[cf].cost);
merge(dp[prv].sol.begin(), dp[prv].sol.end(), dp[cf ^ prv].sol.begin(),
dp[cf ^ prv].sol.end(), dp[cf].sol.begin());
}
int main()
{
scanf("%d%d", &n, &t);
for (int i = 0; i < n; ++ i)
scanf("%d%d", &b[i].c, &b[i].v);
int all = (1 << n) - 1;
for (int cf = 1; cf <= all; ++ cf)
{
dp[cf].cost = n;
if (!vis[cf])
{
vis[cf] = true;
for (int bit = 0; bit < n; ++ bit)
if (cf & (1 << bit))
{
dp[cf].a = MinVal(bit, cf ^ (1 << bit));
break;
}
}
if (dp[cf].a > t)
{
int compCf = all ^ cf;
for (int prvCf = compCf; prvCf != 0; prvCf = compCf & (prvCf - 1))
{
vis[prvCf | cf] = true;
dp[prvCf | cf].a = t + 1;
}
Update(cf);
}
else
{
dp[cf].sol.resize(1);
dp[cf].sol[0] = dp[cf].a;
dp[cf].cost = 1;
}
}
printf("%d\n", dp[all].cost);
for (int ans : dp[all].sol)
printf("%d ", ans);
return 0;
}