text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
char op[110];
int main() {
char c;
int pos = 0, tot;
int add = 0, sub = 0;
for (; cin >> c;) {
if (c == '+')
op[pos++] = '+', add++;
else if (c == '-')
op[pos++] = '-', sub++;
else if (c == '=') {
cin >> tot;
break;
}
}
add++;
int fin = tot + sub;
int ssub = 1;
while (fin < add && sub) fin += sub, ssub++;
int tmp = fin / add;
int cnt = fin % add;
int temp = tmp + (cnt > 0 ? 1 : 0);
if (temp > tot || tmp == 0 || ssub > tot)
printf("Impossible\n");
else {
printf("Possible\n");
if (cnt) {
cout << temp;
cnt--;
} else
cout << tmp;
for (int i = 0; i < pos; i++) {
if (op[i] == '+') {
if (cnt) {
cout << " + " << temp;
cnt--;
} else
cout << " + " << tmp;
} else if (op[i] == '-')
cout << " - " << ssub;
}
cout << " = " << tot << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100233;
char s[maxn];
int v[maxn];
int i, j, k, n, m;
int ra, fh;
char rx;
inline int read() {
rx = getchar(), ra = 0, fh = 1;
while (rx < 48) rx == '-' && (fh = -1), rx = getchar();
while (rx > 47) ra = ra * 10 + rx - 48, rx = getchar();
return ra * fh;
}
int main() {
gets(s + 1);
n = strlen(s + 1);
int n1 = 1, n2 = 0, mx = 0, base = 1;
v[1] = 1;
for (i = 1; i <= n; i++)
if (s[i] == '+')
n1++, v[n1 + n2] = 1;
else if (s[i] == '-')
n2++, v[n1 + n2] = -1;
for (i = n; s[i] >= '0'; i--) mx += base * (s[i] - '0'), base *= 10;
if (n1 * mx - n2 < mx || n1 - n2 * mx > mx) return puts("Impossible"), 0;
puts("Possible");
int sm = n1 + n2, now = 0;
for (i = 1; i <= sm; i++) {
if (i > 1) {
if (v[i] == 1)
printf("+ ");
else
printf("- ");
}
if (v[i] == 1)
n1--;
else
n2--;
int x = mx + n2 - n1 * mx, y = mx + n2 * mx - n1;
for (j = 1; j <= mx; j++) {
now += v[i];
if (now >= x && now <= y) break;
}
printf("%d ", j);
}
printf("= %d\n", mx);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[3];
int cnt1 = 1, cnt2 = 0, n;
vector<int> vec, v1, v2;
while (true) {
scanf("%s", s);
if (s[0] == '+')
++cnt1, vec.push_back(1);
else if (s[0] == '-')
++cnt2, vec.push_back(-1);
else if (s[0] == '=') {
scanf("%d", &n);
break;
}
}
int l1 = cnt1, r1 = cnt1 * n, l2 = cnt2 + n, r2 = cnt2 * n + n;
if (l2 > r1 || l1 > r2) return 0 * puts("Impossible");
puts("Possible");
int sum;
if (l2 >= l1 && l2 <= r1)
sum = l2;
else
sum = l1;
for (int i = 0; i < cnt1; ++i) v1.push_back(1);
int x = sum - cnt1;
for (int i = 0; i < cnt1; ++i) {
int add = min(n - 1, x);
x -= add, v1[i] += add;
if (!x) break;
}
sum = sum - n;
for (int i = 0; i < cnt2; ++i) v2.push_back(1);
x = sum - cnt2;
for (int i = 0; i < cnt2; ++i) {
int add = min(n - 1, x);
x -= add, v2[i] += add;
if (!x) break;
}
int now1 = 0, now2 = 0;
printf("%d ", v1[now1++]);
for (int i = 0; i < (int)vec.size(); ++i) {
if (vec[i] == 1)
printf("%c %d ", '+', v1[now1++]);
else if (vec[i] == -1)
printf("%c %d ", '-', v2[now2++]);
}
printf("%c %d\n", '=', n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, add = 1, sub = 0;
string s;
getline(cin, s);
for (int i = 0; i < (int)s.size(); i++) {
if (isdigit(s[i])) {
n = stoi(s.substr(i, s.size() - i));
break;
} else if (s[i] == '+') {
add++;
} else if (s[i] == '-') {
sub++;
}
}
if (n > n * add - sub || n < add - n * sub) {
cout << "Impossible" << endl;
return 0;
}
int N = add + sub;
cout << "Possible" << endl;
vector<int> res(N), fl(N);
for (int i = 2, j = 1; s[i] != '='; i += 4, j++) {
fl[j] = s[i] == '-';
}
int tmp = n;
for (int i = 0; i < N; i++) {
if (fl[i]) {
sub--;
res[i] = min(n, (n * add - sub) - tmp);
tmp += res[i];
} else {
add--;
res[i] = max(1, tmp - (n * add - sub));
tmp -= res[i];
}
}
int t = 0;
for (auto c : s) {
if (c == '?') {
cout << res[t++];
} else {
cout << c;
}
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int a[110], b[110];
int main() {
char c;
int n;
v.push_back(1);
while (cin >> c) {
cin >> c;
if (c == '+') v.push_back(1);
if (c == '-') v.push_back(-1);
if (c == '=') {
cin >> n;
break;
}
}
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < v.size(); ++i)
if (v[i] == 1)
cnt1++;
else
cnt2++;
int maxn = cnt1 * n - cnt2;
int minv = max(0, cnt1 - cnt2 * n);
if (maxn < n || n < minv) {
cout << "Impossible" << endl;
return 0;
} else {
for (int i = 1; i <= cnt1; ++i) a[i] = n;
for (int i = 1; i <= cnt2; ++i) b[i] = 1;
int cur = maxn;
for (int i = 1; i <= cnt1; ++i) {
if (cur - n <= n - 1) {
a[i] -= (cur - n);
cur = n;
} else {
a[i] -= (n - 1);
cur -= (n - 1);
}
if (cur == n) break;
}
if (cur != n) {
for (int i = 1; i <= cnt2; ++i) {
if (cur - n <= n - 1) {
b[i] += (cur - n);
cur = n;
} else {
b[i] += (n - 1);
cur -= (n - 1);
}
if (cur == n) break;
}
}
int p = 2, q = 1;
cout << "Possible" << endl;
cout << a[1];
for (int i = 1; i < v.size(); ++i) {
if (v[i] == 1)
cout << " + " << a[p++];
else
cout << " - " << b[q++];
}
cout << " = " << n << endl;
}
}
|
#include <bits/stdc++.h>
volatile bool isLocalTestEnabled = 0;
bool g_isLocalPrintEnabled = (bool)(0);
template <typename T>
void UpdateMin(T& a, const T b) {
a = std::min(a, b);
}
template <typename T>
void UpdateMax(T& a, const T b) {
a = std::max(a, b);
}
const long double Pi = std::atan(1.0L) * 4.0L;
static const long double Eps = 1.0e-09;
template <typename T>
bool IsEqual(const T a, const T b) {
return std::abs(a - b) < Eps;
}
template <typename T>
bool IsGreater(const T a, const T b) {
return a > b + Eps;
}
template <typename T>
bool IsLess(const T a, const T b) {
return a + Eps < b;
}
template <typename T>
bool IsGreaterEqual(const T a, const T b) {
return !IsLess(a, b);
}
template <typename T>
bool IsLessEqual(const T a, const T b) {
return !IsGreater(a, b);
}
template <typename T>
std::string ToStr(const T& val) {
std::ostringstream ostr;
ostr << val;
return ostr.str();
}
template <typename T>
bool FromStr(const std::string& str, T& val) {
std::istringstream istr(str);
istr >> val;
return !!istr;
}
template <typename T>
std::istream& operator>>(std::istream& ist, std::vector<T>& data) {
;
for (size_t i = 0; i < data.size(); i++) {
ist >> data[i];
}
return ist;
}
template <typename T>
T Read(std::istream& ist) {
;
T val;
ist >> val;
return val;
}
template <typename T>
std::ostream& operator<<(std::ostream& ost, const std::vector<T>& data) {
for (size_t i = 0; i < data.size(); i++) {
if (i != 0) {
ost << ' ';
}
ost << data[i];
}
return ost;
}
template <size_t id>
class StopWatch {};
void Parse(const std::string& line, std::vector<int32_t>& signs, int64_t& sum) {
signs.clear();
signs.push_back(1);
sum = 0;
size_t i = 0;
while (i < line.size()) {
if (line[i] == '+') {
signs.push_back(1);
} else if (line[i] == '-') {
signs.push_back(-1);
} else if (line[i] == '=') {
i++;
break;
}
i++;
}
const bool isSumParsed = FromStr<int64_t>(line.substr(i), sum);
;
}
bool IsInRange(const int64_t v, const int64_t vMin, const int64_t vMax) {
return (v >= vMin) && (v <= vMax);
}
bool TryFindSolution(const int64_t n, const int64_t pluses,
const int64_t minuses, const int64_t sum, int64_t& toAdd,
int64_t& toSubtract) {
toAdd = pluses;
toSubtract = minuses;
const int64_t minPlus = pluses * 1;
const int64_t maxPlus = pluses * n;
const int64_t minMinus = minuses * 1;
const int64_t maxMinus = minuses * n;
if (IsInRange(minPlus - sum, minMinus, maxMinus)) {
toAdd = minPlus;
toSubtract = minPlus - sum;
return true;
}
if (IsInRange(maxPlus - sum, minMinus, maxMinus)) {
toAdd = maxPlus;
toSubtract = maxPlus - sum;
return true;
}
if (IsInRange(sum + minMinus, minPlus, maxPlus)) {
toAdd = sum + minMinus;
toSubtract = minMinus;
return true;
}
return false;
}
std::vector<int64_t> ConstructSummands(const int64_t n, const int64_t count,
const int64_t totalToAdd) {
std::vector<int64_t> summands((size_t)count, 1);
;
int64_t rem = totalToAdd - count;
for (size_t i = 0; i < summands.size(); i++) {
if (rem > 0) {
const int64_t d = std::min(rem, n - 1);
summands[i] += d;
rem -= d;
};
;
};
return summands;
}
std::string ConstructSolution(const int64_t n,
const std::vector<int32_t>& signs,
const int64_t pluses, const int64_t minuses,
const int64_t sum, const int64_t toAdd,
const int64_t toSubtract) {
;
std::vector<int64_t> itemsToAdd = ConstructSummands(n, pluses, toAdd);
std::vector<int64_t> itemsToSubtract =
ConstructSummands(n, minuses, toSubtract);
;
;
std::ostringstream ostr;
size_t iAdd = 0;
size_t iSubtract = 0;
for (size_t i = 0; i < signs.size(); i++) {
if (signs[i] == 1) {
if (i != 0) {
ostr << " + ";
}
ostr << itemsToAdd[iAdd];
iAdd++;
} else if (signs[i] == -1) {
;
ostr << " - ";
ostr << itemsToSubtract[iSubtract];
iSubtract++;
}
}
ostr << " = " << sum;
return ostr.str();
}
bool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode) {
StopWatch<1> sw;
(void)sw;
std::string line;
std::getline(ist, line);
if (multipleTestMode && !ist) return false;
if (!g_isLocalPrintEnabled) {
} else
std::cerr << std::endl << "Next test: " << line << std::endl;
std::vector<int32_t> signs;
int64_t n = 0;
Parse(line, signs, n);
;
const int64_t pluses = std::count(signs.begin(), signs.end(), 1);
const int64_t minuses = std::count(signs.begin(), signs.end(), -1);
int64_t toAdd;
int64_t toSubtract;
if (!TryFindSolution(n, pluses, minuses, n, toAdd, toSubtract)) {
ost << "Impossible" << std::endl;
return multipleTestMode;
}
const std::string solved =
ConstructSolution(n, signs, pluses, minuses, n, toAdd, toSubtract);
ost << "Possible" << std::endl;
ost << solved << std::endl;
;
return multipleTestMode;
}
int main(int argc, const char* argv[]) {
std::ios_base::sync_with_stdio(false);
std::istream* ist = &std::cin;
std::ostream* ost = &std::cout;
std::unique_ptr<std::istream> fileInput;
if (argc > 1) {
fileInput.reset(new std::ifstream(argv[1]));
if (!(*fileInput)) {
std::cout << "File not found: " << argv[1] << std::endl;
}
ist = fileInput.get();
}
Solve(*ist, *ost, false);
}
|
#include <bits/stdc++.h>
using namespace std;
char s[1000];
int pu_cou, mi_cou;
int n;
int maxn, minn;
int pu[1000], mi[1000], sum;
int main() {
gets(s);
pu_cou = 1;
mi_cou = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '+') pu_cou++;
if (s[i] == '-') mi_cou++;
if (s[i] == '=') {
n = atoi(s + i + 1);
}
}
maxn = n * pu_cou - mi_cou;
minn = pu_cou - n * mi_cou;
if (n < minn || n > maxn) {
puts("Impossible");
return 0;
}
for (int i = 0; i < mi_cou; i++) mi[i] = n;
for (int i = 0; i < pu_cou; i++) pu[i] = 1;
sum = minn;
int mipo = 0, pupo = 0;
while (sum < n && mipo < mi_cou) {
mi[mipo] = max(1, sum);
sum += n - mi[mipo++];
}
while (sum < n && pupo < pu_cou) {
pu[pupo] = min(n, 1 + n - sum);
sum += pu[pupo++] - 1;
}
if (sum != n) {
puts("Impossible");
return 0;
}
puts("Possible");
pupo = 1;
mipo = 0;
printf("%d", pu[0]);
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '+') {
printf(" + %d", pu[pupo++]);
}
if (s[i] == '-') {
printf(" - %d", mi[mipo++]);
}
if (s[i] == '=') {
printf(" = %d\n", n);
}
}
}
|
#include <bits/stdc++.h>
volatile bool isLocalTestEnabled = 0;
bool g_isLocalPrintEnabled = (bool)(0);
template <typename T>
void UpdateMin(T& a, const T b) {
a = std::min(a, b);
}
template <typename T>
void UpdateMax(T& a, const T b) {
a = std::max(a, b);
}
const long double Pi = std::atan(1.0L) * 4.0L;
static const long double Eps = 1.0e-09;
template <typename T>
bool IsEqual(const T a, const T b) {
return std::abs(a - b) < Eps;
}
template <typename T>
bool IsGreater(const T a, const T b) {
return a > b + Eps;
}
template <typename T>
bool IsLess(const T a, const T b) {
return a + Eps < b;
}
template <typename T>
bool IsGreaterEqual(const T a, const T b) {
return !IsLess(a, b);
}
template <typename T>
bool IsLessEqual(const T a, const T b) {
return !IsGreater(a, b);
}
template <typename T>
std::string ToStr(const T& val) {
std::ostringstream ostr;
ostr << val;
return ostr.str();
}
template <typename T>
bool FromStr(const std::string& str, T& val) {
std::istringstream istr(str);
istr >> val;
return !!istr;
}
template <typename T>
std::istream& operator>>(std::istream& ist, std::vector<T>& data) {
;
for (size_t i = 0; i < data.size(); i++) {
ist >> data[i];
}
return ist;
}
template <typename T>
T Read(std::istream& ist) {
;
T val;
ist >> val;
return val;
}
template <typename T>
std::ostream& operator<<(std::ostream& ost, const std::vector<T>& data) {
for (size_t i = 0; i < data.size(); i++) {
if (i != 0) {
ost << ' ';
}
ost << data[i];
}
return ost;
}
template <size_t id>
class StopWatch {};
void Parse(const std::string& line, std::vector<int32_t>& signs, int64_t& sum) {
signs.clear();
signs.push_back(1);
sum = 0;
size_t i = 0;
while (i < line.size()) {
if (line[i] == '+') {
signs.push_back(1);
} else if (line[i] == '-') {
signs.push_back(-1);
} else if (line[i] == '=') {
i++;
break;
}
i++;
}
const bool isSumParsed = FromStr<int64_t>(line.substr(i), sum);
;
}
bool IsInRange(const int64_t v, const int64_t vMin, const int64_t vMax) {
return (v >= vMin) && (v <= vMax);
}
bool TryFindSolution(const int64_t n, const int64_t pluses,
const int64_t minuses, const int64_t sum, int64_t& toAdd,
int64_t& toSubtract) {
toAdd = pluses;
toSubtract = minuses;
const int64_t minPlus = pluses * 1;
const int64_t maxPlus = pluses * n;
const int64_t minMinus = minuses * 1;
const int64_t maxMinus = minuses * n;
if (IsInRange(minPlus - sum, minMinus, maxMinus)) {
toAdd = minPlus;
toSubtract = minPlus - sum;
return true;
}
if (IsInRange(maxPlus - sum, minMinus, maxMinus)) {
toAdd = maxPlus;
toSubtract = maxPlus - sum;
return true;
}
if (IsInRange(sum + minMinus, minPlus, maxPlus)) {
toAdd = sum + minMinus;
toSubtract = minMinus;
return true;
}
if (IsInRange(sum + maxMinus, minPlus, maxPlus)) {
toAdd = sum + maxMinus;
toSubtract = maxMinus;
return true;
}
return false;
}
std::vector<int64_t> ConstructSummands(const int64_t n, const int64_t count,
const int64_t totalToAdd) {
std::vector<int64_t> summands((size_t)count, 1);
;
int64_t rem = totalToAdd - count;
for (size_t i = 0; i < summands.size(); i++) {
if (rem > 0) {
const int64_t d = std::min(rem, n - 1);
summands[i] += d;
rem -= d;
};
;
};
return summands;
}
std::string ConstructSolution(const int64_t n,
const std::vector<int32_t>& signs,
const int64_t pluses, const int64_t minuses,
const int64_t sum, const int64_t toAdd,
const int64_t toSubtract) {
;
std::vector<int64_t> itemsToAdd = ConstructSummands(n, pluses, toAdd);
std::vector<int64_t> itemsToSubtract =
ConstructSummands(n, minuses, toSubtract);
;
;
std::ostringstream ostr;
size_t iAdd = 0;
size_t iSubtract = 0;
for (size_t i = 0; i < signs.size(); i++) {
if (signs[i] == 1) {
if (i != 0) {
ostr << " + ";
}
ostr << itemsToAdd[iAdd];
iAdd++;
} else if (signs[i] == -1) {
;
ostr << " - ";
ostr << itemsToSubtract[iSubtract];
iSubtract++;
}
}
ostr << " = " << sum;
return ostr.str();
}
bool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode) {
StopWatch<1> sw;
(void)sw;
std::string line;
std::getline(ist, line);
if (multipleTestMode && !ist) return false;
if (!g_isLocalPrintEnabled) {
} else
std::cerr << std::endl << "Next test: " << line << std::endl;
std::vector<int32_t> signs;
int64_t n = 0;
Parse(line, signs, n);
;
const int64_t pluses = std::count(signs.begin(), signs.end(), 1);
const int64_t minuses = std::count(signs.begin(), signs.end(), -1);
int64_t toAdd;
int64_t toSubtract;
if (!TryFindSolution(n, pluses, minuses, n, toAdd, toSubtract)) {
ost << "Impossible" << std::endl;
return multipleTestMode;
}
const std::string solved =
ConstructSolution(n, signs, pluses, minuses, n, toAdd, toSubtract);
ost << "Possible" << std::endl;
ost << solved << std::endl;
;
return multipleTestMode;
}
int main(int argc, const char* argv[]) {
std::ios_base::sync_with_stdio(false);
std::istream* ist = &std::cin;
std::ostream* ost = &std::cout;
std::unique_ptr<std::istream> fileInput;
if (argc > 1) {
fileInput.reset(new std::ifstream(argv[1]));
if (!(*fileInput)) {
std::cout << "File not found: " << argv[1] << std::endl;
}
ist = fileInput.get();
}
Solve(*ist, *ost, false);
}
|
#include <bits/stdc++.h>
using namespace std;
int i, ans[1000005], ans1[1000005];
char s[1000005];
vector<int> add, minu;
int main() {
gets(s);
int l = strlen(s);
int val = 0;
add.push_back(0);
for (i = 0; i < l; i++) {
if (s[i] == '+')
add.push_back(i);
else if (s[i] == '-')
minu.push_back(i);
else if (s[i] >= '0' && s[i] <= '9') {
val = val * 10 + (s[i] - '0');
}
}
if (add.size() * val < val + minu.size()) return 0 * printf("Impossible\n");
int tot = val + minu.size() - add.size();
int extra = 0;
if (tot < 0) {
extra = -tot;
tot = 0;
if (extra > (long long)minu.size() * (val - 1))
return 0 * printf("Impossible\n");
}
for (i = 0; i < add.size(); i++) {
int now = min(tot, val - 1);
ans[i] = now + 1;
tot -= now;
}
for (i = 0; i < minu.size(); i++) {
int now = min(extra, val - 1);
extra -= now;
ans1[i] = now + 1;
}
int p = 0;
int p1 = 0;
int cnt = 0;
printf("Possible\n");
printf("%d", ans[p++]);
for (i = 0; i < l; i++) {
if (s[i] == '+') {
printf(" %c %d", s[i], ans[p++]);
} else if (s[i] == '-') {
printf(" %c %d", s[i], ans1[p1++]);
}
}
printf(" = %d", val);
printf("\n");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char st[2000];
gets(st);
int add, sub, n;
vector<int> toAdd, toSub;
add = sub = 0;
add++;
for (int i = 0; st[i]; i++) {
if (st[i] >= '0' && st[i] <= '9') {
n = atoi(st + i);
break;
}
if (st[i] == '+')
add++;
else if (st[i] == '-')
sub++;
}
if (n * add - sub < n)
puts("Impossible");
else if (add - sub * n > n)
puts("Impossible");
else {
puts("Possible");
if (add == sub + 1) {
printf("%d", n);
for (int i = 1; st[i]; i++) {
if (st[i] == '?')
printf("1");
else
printf("%c", st[i]);
}
printf("\n");
} else if (sub >= add) {
printf("%d", n);
add--;
for (int i = 1; st[i]; i++) {
if (st[i] == '?') {
if (st[i - 2] == '-')
printf("1");
else if (st[i - 2] == '+') {
printf("%d", sub / add);
sub -= sub / add;
add--;
}
} else
printf("%c", st[i]);
}
printf("\n");
} else {
for (int i = 0; i < add; i++) toAdd.push_back(n * (sub + 1) / add);
add = (n * (sub + 1)) % add;
for (int i = 0; i < add; i++) toAdd[i]++;
int curPos = 1;
printf("%d", toAdd[0]);
for (int i = 1; st[i]; i++) {
if (st[i] == '?') {
if (st[i - 2] == '-')
printf("%d", n);
else if (st[i - 2] == '+') {
printf("%d", toAdd[curPos++]);
}
} else
putchar(st[i]);
}
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[5];
int x, y, n, c = 0;
bool v[105];
int main() {
x++;
v[0] = 1;
while (scanf("%s", s) != EOF) {
if (strcmp(s, "+") == 0) {
x++;
v[++c] = 1;
} else if (strcmp(s, "-") == 0) {
y++;
c++;
} else if (strcmp(s, "=") == 0)
break;
}
scanf("%d", &n);
if (n * x - y < n || x - n * y > n) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
int m = n;
bool ok = 0;
for (int i = 0; i <= c; i++) {
if (v[i]) {
x--;
if (ok) printf("+ ");
for (int j = 1; j <= n; j++) {
if (j + n * x - y >= m && j + x - n * y <= m) {
printf("%d ", j);
m -= j;
break;
}
}
} else {
y--;
if (ok) printf("- ");
for (int j = 1; j <= n; j++) {
if (-j + n * x - y >= m && -j + x - n * y <= m) {
printf("%d ", j);
m += j;
break;
}
}
}
ok = 1;
}
printf("= %d\n", n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> mns, pls;
string s;
int num, dir, i, now, n, j;
int x[1000020], ans[1000200];
int main() {
cin >> s;
dir = 1;
while (s != "=") {
num++;
x[num] = dir;
if (dir)
pls.push_back(num);
else
mns.push_back(num);
cin >> s;
if (s == "+") {
dir = 1;
cin >> s;
}
if (s == "-") {
dir = 0;
cin >> s;
}
}
cin >> n;
now = -n + pls.size() - mns.size();
for (i = 1; i <= num; i++) ans[i] = 1;
if (now < 0) {
for (j = 0; j < pls.size(); j++) {
ans[pls[j]] += min(n - 1, -now);
now += min(n - 1, -now);
}
}
if (now > 0) {
for (j = 0; j < mns.size(); j++) {
ans[mns[j]] += min(n - 1, now);
now -= min(n - 1, now);
}
}
if (now != 0) {
cout << "Impossible" << endl;
return 0;
}
cout << "Possible" << endl;
cout << ans[1];
for (j = 2; j <= num; j++)
if (x[j])
cout << " + " << ans[j];
else
cout << " - " << ans[j];
cout << " = " << n << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = (1ll << 62) - 1;
const long long mod = 1e9 + 7;
const long long N = 2e6 + 10;
const long double pi = 3.14159265358979323846;
long long bPow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) {
res = (res * a) % mod;
}
b >>= 1;
a = (a * a) % mod;
}
return res % mod;
}
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
if (b == 0) return a;
return gcd(b, a % b);
}
vector<long long> fact(N, 0ll);
void factorial() {
fact[0] = 1, fact[1] = 1;
for (long long i = 2; i < N; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
}
long long ncr(long long n, long long r) {
if (r > n) return 0;
long long ans = fact[n] % mod;
ans *= bPow(fact[r], mod - 2) % mod;
ans %= mod;
ans *= bPow(fact[n - r], mod - 2) % mod;
ans %= mod;
return ans;
}
vector<long long> primes(N, -1);
void sieve() {
iota(primes.begin(), primes.end(), 0);
for (long long i = 2; i * i <= N; ++i) {
if (primes[i] == i) {
for (long long j = 2 * i; j < N; j += i) {
primes[j] = i;
}
}
}
}
void solve() {
string nn;
long long ctAdd = 0, ctNeg = 0;
string seq;
while (true) {
string s;
cin >> s;
if (s == " " or s == "=" or s == "?") continue;
if (s == "+") {
seq += '+';
ctAdd++;
} else if (s == "-") {
seq += '-';
ctNeg++;
} else {
nn = s;
break;
}
}
long long n = 0, pow = 1;
reverse(nn.begin(), nn.end());
for (char c : nn) {
long long temp = c - '0';
n += temp * pow;
pow *= 10;
}
reverse(nn.begin(), nn.end());
vector<long long> add, neg;
ctAdd++;
long long diff = ctAdd - ctNeg;
for (long long i = 0; i < ctAdd; ++i) {
add.push_back(1);
}
for (long long i = 0; i < ctNeg; ++i) {
neg.push_back(1);
}
if (diff < n) {
for (long long i = 0; i < ctAdd and diff != n; ++i) {
long long cur = min(n - diff, n - 1);
add[i] += cur;
diff += cur;
}
}
if (diff > n) {
for (long long i = 0; i < ctNeg and diff != n; ++i) {
long long cur = min(diff - n, n - 1);
neg[i] += cur;
diff -= cur;
}
}
if (diff != n) {
cout << "Impossible"
<< " \n";
return;
}
cout << "Possible"
<< " \n"
<< add[0] << " ";
long long i = 1, j = 0;
for (char c : seq) {
if (c == '+') {
cout << c << " " << add[i++] << " ";
} else {
cout << c << " " << neg[j++] << " ";
}
}
cout << "= " << n << " \n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tc = 1, test = 1;
while (tc--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int a[100500];
int b[100500];
int main() {
string s;
int k = 0;
while (true) {
cin >> s;
cin >> s;
if (s[0] == '=') {
break;
} else if (s[0] == '+') {
a[k++] = 0;
} else if (s[0] == '-') {
a[k++] = 1;
}
}
int n;
cin >> n;
int min_s = 1;
int max_s = n;
for (int i = 0; i < k; i++)
if (a[i] == 0) {
min_s += 1;
max_s += n;
} else {
min_s -= n;
max_s--;
}
if (min_s > n || max_s < n) {
cout << "Impossible" << endl;
return 0;
}
int ss = n;
cout << "Possible" << endl;
for (int i = 0; i <= k; i++) b[i] = 1;
n--;
for (int i = 0; i < k; i++)
if (a[i])
n++;
else
n--;
if (n > 0) {
b[0] = min(ss, n + 1);
n -= b[0] - 1;
}
for (int i = 1; i <= k; i++)
if (n > 0 && a[i - 1] == 0) {
b[i] = min(ss, n + 1);
n -= b[i] - 1;
} else if (n < 0 && a[i - 1] == 1) {
b[i] = min(ss, -n + 1);
n += b[i] - 1;
}
cout << b[0] << ' ';
for (int i = 1; i <= k; i++) {
if (a[i - 1] == 0)
cout << "+ ";
else
cout << "- ";
cout << b[i] << " ";
}
cout << "= " << ss << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, add, de, l, r;
int base = 0, now;
int solve() {
r = base * add - de;
l = -base * de + add;
int l1, r1, mid;
if (now == 0)
l1 = n - base, r1 = n - 1;
else
l1 = n + 1, r1 = n + base;
mid = (l1 + r1) >> 1;
int cnt = 0;
while (l1 <= r1) {
mid = (l1 + r1) >> 1;
int l2 = mid + l;
int r2 = mid + r;
if (base < l2)
r1 = mid - 1;
else if (base > r2)
l1 = mid + 1;
else {
break;
}
}
int a = n;
n = mid;
return fabs(mid - a);
}
bool check() {
r = base * add - de;
l = -base * de + add;
if (base <= r && base >= l) return 1;
return 0;
}
int main() {
add = de = 0;
string s;
getline(cin, s);
int l = s.size();
add++;
for (int i = 0; i < l; i++) {
if (s[i] == '-') de++;
if (s[i] == '+') add++;
}
stringstream ss(s);
string t;
while (ss >> t && t[0] != '=')
;
ss >> n;
base = n;
n = 0;
if (!check()) {
puts("Impossible");
} else {
puts("Possible");
int pos = 0;
now = 1;
while (pos < l) {
if (s[pos] == '=') {
printf("=");
pos++;
break;
}
if (s[pos] == '?') {
if (now)
add--;
else
de--;
printf("%d", solve());
} else
printf("%c", s[pos]);
if (s[pos] == '-') now = 0;
if (s[pos] == '+') now = 1;
pos++;
}
while (pos < l) printf("%c", s[pos++]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void cline() { cout << '\n'; }
template <typename T, typename... V>
void cline(T t, V... v) {
cout << t;
if (sizeof...(v)) cout << ' ';
cline(v...);
}
void cspc() { cout << ' '; }
template <typename T, typename... V>
void cspc(T t, V... v) {
cout << t;
if (sizeof...(v)) cout << ' ';
cspc(v...);
}
const int N = 1e6 + 4;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
getline(cin, s);
int frep = 0, fren = 0;
int n = s.size();
bool flag = 1;
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
if (flag) {
frep++;
flag = 0;
} else {
fren++;
}
} else if (s[i] == '+') {
flag = 1;
}
}
int last = 0, fac = 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == ' ') {
break;
}
int num = s[i] - '0';
last += fac * num;
fac = fac * 10;
}
if ((frep > (fren + 1) * last) || (fren > (frep - 1) * last)) {
cspc("Impossible");
return 0;
}
cline("Possible");
vector<int> vp, vn;
for (int i = 0; i < fren; i++) {
vn.push_back(1);
}
for (int i = 0; i < frep; i++) {
vp.push_back(1);
}
if (fren + last - frep > 0) {
int extra = fren + last - frep;
int add = extra / frep;
int perc = extra % frep;
for (int i = 0; i < frep; i++) {
if (i < perc) {
vp[i] += add + 1;
} else {
vp[i] += add;
}
}
} else if (fren + last - frep < 0) {
int extra = frep - fren - last;
int add = extra / fren;
int perc = extra % fren;
for (int i = 0; i < fren; i++) {
if (i < perc) {
vn[i] += add + 1;
} else {
vn[i] += add;
}
}
}
int j = 0, k = 0;
flag = 1;
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
if (flag) {
cout << vp[j];
j++;
flag = 0;
} else {
cout << vn[k];
k++;
}
} else {
if (s[i] == '+') {
flag = 1;
}
cout << s[i];
}
}
return 0;
}
|
#include <bits/stdc++.h>
const int INF_INT = 0x3f3f3f3f;
const long long INF_LL = 0x7f7f7f7f;
const int MOD = 1e9 + 7;
const double eps = 1e-10;
const double pi = acos(-1);
using namespace std;
int n;
int main(int argc, char const *argv[]) {
string s, t;
int pos = 0;
int neg = 0;
queue<char> q;
while (1) {
cin >> s >> t;
if (t == "=")
break;
else if (t == "+")
pos++;
else if (t == "-")
neg++;
q.push(t[0]);
}
scanf("%d", &n);
if (!pos && !neg) {
printf("Possible\n");
printf("%d = %d\n", n, n);
return 0;
}
if (pos - n * neg > n || n * (pos + 1) - neg < n) {
printf("Impossible\n");
return 0;
} else {
puts("Possible");
if (pos == neg) {
printf("%d", n);
while (q.size()) {
printf(" %c 1", q.front());
q.pop();
}
printf(" = %d\n", n);
} else if (pos > neg) {
int tmp = abs(pos - neg);
queue<int> qq;
for (int i = 1; i <= neg; i++) {
if (tmp >= n - 1)
qq.push(n), tmp -= n - 1;
else
qq.push(1 + tmp), tmp = 0;
}
printf("%d", n - tmp);
while (q.size()) {
char c = q.front();
q.pop();
if (c == '-')
printf(" - %d", qq.front()), qq.pop();
else
printf(" + 1");
}
printf(" = %d\n", n);
} else if (pos < neg) {
printf("%d", n);
int tmp = abs(pos - neg);
queue<int> qq;
for (int i = 1; i <= pos; i++) {
if (tmp >= n - 1)
qq.push(n), tmp -= n - 1;
else
qq.push(1 + tmp), tmp = 0;
}
while (q.size()) {
char c = q.front();
q.pop();
if (c == '+')
printf(" + %d", qq.front()), qq.pop();
else
printf(" - 1");
}
printf(" = %d\n", n);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int np = 1;
int nm = 0;
int cnt = 0;
int a[110];
a[0] = 1;
while (1) {
string s;
cin >> s;
if (s == "=") break;
if (s == "+") {
np++;
a[++cnt] = 1;
}
if (s == "-") {
nm++;
a[++cnt] = -1;
}
}
int n;
cin >> n;
if ((np * n - nm < n) || (np - nm * n > n)) {
cout << "Impossible" << endl;
return 0;
}
int sum = n + nm - np;
cout << "Possible" << endl;
for (int i = 0; i <= cnt; i++) {
if ((i > 0) && (a[i] > 0)) cout << " + ";
if (a[i] < 0) cout << " - ";
if (sum < 0) {
if (a[i] < 0) {
if (sum < -(n - 1)) {
sum += (n - 1);
cout << n;
} else {
cout << (-sum + 1);
sum = 0;
}
} else
cout << "1";
} else {
if (a[i] > 0) {
if (sum > (n - 1)) {
sum -= (n - 1);
cout << n;
} else {
cout << (sum + 1);
sum = 0;
}
} else
cout << "1";
}
}
cout << " = " << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MaxN = 2E5 + 10;
const long double eps = 1E-8;
const long long INF = 1E9 + 7;
const long long MOD = 1000 * 1000 * 1000 + 7;
vector<char> pm;
vector<long long> num;
long long bpow(long long n, long long k) {
if (k == 0) return 1;
long long cnt = bpow(n, k / 2);
if (k % 2 == 0)
return (cnt * cnt) % MOD;
else
return (n * cnt * cnt) % MOD;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cout << fixed << setprecision(12);
string s;
getline(cin, s);
pm.push_back('+');
for (long long i = 0; i < s.size(); ++i)
if (s[i] == '-' || s[i] == '+') pm.push_back(s[i]);
long long ind = s.size() - 1;
while (s[ind] >= '0' && s[ind] <= '9') {
num.push_back(s[ind] - '0');
--ind;
}
reverse(num.begin(), num.end());
long long n = 0;
for (long long i = 0; i < num.size(); ++i) {
n *= 10;
n += num[i];
}
num.clear();
long long cnt = 0;
for (long long i = 0; i < pm.size(); ++i) {
if (pm[i] == '+') {
cnt += n;
num.push_back(n);
} else {
--cnt;
num.push_back(1);
}
}
if (cnt < n) {
cout << "Impossible";
return 0;
}
cnt -= n;
for (long long i = 0; i < num.size(); ++i) {
if (pm[i] == '+') {
long long x = min(cnt, num[i] - 1);
num[i] -= x;
cnt -= x;
} else {
long long x = min(cnt, n - num[i]);
num[i] += x;
cnt -= x;
}
}
if (cnt) {
cout << "Impossible";
return 0;
}
cout << "Possible" << endl << num[0] << " ";
for (long long i = 1; i < num.size(); ++i)
cout << pm[i] << " " << num[i] << " ";
cout << "= " << n;
}
|
#include <bits/stdc++.h>
using namespace std;
char c[100000];
char k[1000000];
int pos = 1;
int neg = 0;
int main() {
int j = 0;
while (true) {
char c;
cin >> c;
cin >> c;
if (c == '=') break;
if (c == '+') pos++;
if (c == '-') neg++;
k[j++] = c;
}
int n;
cin >> n;
int mini = (1 * pos - neg * n);
int maxii = (pos * n - neg * 1);
if (mini <= n && n <= maxii) {
cout << "Possible" << endl;
} else {
cout << "Impossible" << endl;
return 0;
}
int sum = 0;
for (int i = 0; i < j; i++) {
int sgn = 1;
if (i > 0 && k[i - 1] == '-') sgn = -1;
if (sgn == -1) neg--;
if (sgn == 1) pos--;
for (int x = 1; x <= n; x++) {
if (sum + sgn * x + n * pos - neg >= n &&
n >= sum + sgn * x + 1 * pos - n * neg) {
sum += x * sgn;
cout << x << " " << k[i] << " ";
break;
}
}
}
cout << abs(n - sum) << " = " << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
char in[10];
int i, j, k, n;
vector<int> plus, minus, order;
cin >> in;
order.push_back(1);
plus.push_back(1);
cin >> in;
while (in[0] != '=') {
if (in[0] == '+')
plus.push_back(1), order.push_back(1);
else
minus.push_back(1), order.push_back(-1);
cin >> in;
cin >> in;
}
int ans = int(plus.size()) - int(minus.size());
cin >> n;
if (n * int(plus.size()) - int(minus.size()) < n ||
int(plus.size()) - n * int(minus.size()) > n) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
i = 0, j = 0;
while (ans != n) {
if (n - ans >= n - 1) {
plus[i++] += n - 1;
ans += n - 1;
} else if (n - ans > 0) {
plus[i++] += n - ans;
ans += (n - ans);
} else if (n - ans <= -(n - 1)) {
minus[j++] += n - 1;
ans -= n - 1;
} else {
minus[j++] += -(n - ans);
ans += n - ans;
}
}
j = k = 0;
printf("%d ", plus[j++]);
for (i = 1; i < int(order.size()); i++) {
if (order[i] == 1) {
printf("+ %d ", plus[j++]);
} else {
printf("- %d ", minus[k++]);
}
}
printf("= %d\n", n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char c;
int a[110], n, num, p;
int main() {
p = 1;
num = 0;
a[++num] = 1;
while (scanf("%c", &c)) {
if (c == '\n') break;
if (c == '-') p--, a[++num] = -1;
if (c == '+') p++, a[++num] = 1;
if (c >= '0' && c <= '9') {
n *= 10;
n += c - '0';
}
}
for (int i = 1; i <= num; i++) {
while ((p < n) and (a[i] > 0) and (a[i] < n)) a[i]++, p++;
while ((p > n) and (a[i] < 0) and (a[i] > -n)) a[i]--, p--;
}
if (p != n) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
for (int i = 1; i <= num; i++)
cout << (i > 1 ? (a[i] < 0 ? "- " : "+ ") : "") << abs(a[i]) << " ";
cout << "= " << n;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
char qs[300];
int n;
int main(int argc, char **argv) {
ios_base::sync_with_stdio(0);
int index = 0;
while (cin >> qs[index] >> qs[index] && qs[index] != '=') index++;
cin >> n;
int pcount = 1, mcount = 0;
for (int i = 0; qs[i] != '='; i++) {
pcount += qs[i] == '+';
mcount += qs[i] == '-';
}
if (n > n * pcount - mcount || n < pcount - n * mcount) {
cout << "Impossible" << endl;
return 0;
}
vector<int> res(pcount + mcount, 1);
int sum = pcount - mcount;
int j = 0;
while (sum < n) {
if (res[j] < n) {
res[j]++;
sum++;
} else {
do {
j++;
} while (qs[j - 1] != '+');
}
}
j = 0;
do {
j++;
} while (qs[j - 1] != '-');
while (sum > n) {
if (res[j] < n) {
res[j]++;
sum--;
} else {
do {
j++;
} while (qs[j - 1] != '-');
}
}
cout << "Possible\n";
for (int i = 0; i < (int)(res.size()); i++) {
cout << res[i] << " " << qs[i] << " ";
}
cout << n;
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int res, p, m, ans[105];
bool pos[105];
int main() {
int mx = 0, mn = 0;
getline(cin, s);
int cnt = 1;
for (int i = s.size() - 1; s[i] != ' '; --i) {
res += (s[i] - '0') * cnt;
cnt *= 10;
}
p = 1;
cnt = 1;
for (int i = 0; s[i] != '='; ++i) {
if (s[i] == '+') {
p++;
pos[++cnt] = true;
} else if (s[i] == '-') {
m++;
pos[++cnt] = false;
}
}
mx = p * res - m;
mn = p - m * res;
if (res > mx || res < mn) {
puts("Impossible");
return 0;
}
puts("Possible");
int lp = (res < p ? p : res), rp = res * p;
int lm = m, rm = res * m;
int x, y;
for (int i = lp; i <= rp; ++i) {
if (i - res >= lm && i - res <= rm) {
x = i;
y = i - res;
break;
}
}
pos[1] = true;
int q = 0;
for (int i = 1; i <= cnt; ++i) {
if (pos[i]) {
if (x - (p - 1) <= res) {
ans[i] = x - (p - 1);
x -= ans[i];
} else {
ans[i] = res;
x -= ans[i];
}
q += ans[i];
--p;
} else {
if (y - (m - 1) <= res) {
ans[i] = y - (m - 1);
y -= ans[i];
} else {
ans[i] = res;
y -= ans[i];
}
q -= ans[i];
--m;
}
if (i != 1) cout << (pos[i] ? '+' : '-') << ' ';
cout << ans[i] << ' ';
}
cout << "= " << res;
}
|
#include <bits/stdc++.h>
using namespace std;
string s, str;
long long mns, all, pls, nom, maxi, mini, n, sum, otn;
vector<char> v;
vector<long long> ans;
int main() {
getline(cin, s);
for (int i = 0; i < s.size(); i++) {
if (s[i] == '-') mns++;
if (s[i] == '?') all++;
if (s[i] == '=') nom = i;
}
pls = all - mns;
string str;
str = s.substr(nom + 2, s.size() - nom - 2);
n = atoll(str.c_str());
maxi = n * pls - mns;
mini = pls - mns * n;
if (n < mini || n > maxi) {
cout << "Impossible";
return 0;
}
cout << "Possible\n";
v.push_back('+');
for (int i = 0; i < s.size(); i++)
if (s[i] == '+')
v.push_back('+');
else if (s[i] == '-')
v.push_back('-');
for (int i = 0; i < v.size(); i++)
if (v[i] == '+')
ans.push_back(n);
else
ans.push_back(1);
sum = maxi;
for (int i = 0; i < ans.size(); i++)
if (sum > n)
if (v[i] == '+') {
otn = min(n - 1, sum - n);
ans[i] -= otn;
sum -= otn;
} else {
otn = min(n - 1, sum - n);
ans[i] += otn;
sum -= otn;
}
cout << ans[0] << " ";
for (int i = 1; i < v.size(); i++) cout << v[i] << " " << ans[i] << " ";
cout << "= " << n;
return 0;
}
|
#include <bits/stdc++.h>
int main() {
long long int n = 0, p = 1, m = 0, num, sign[105], s = 0;
char c = 0;
while (c != '=') {
scanf("%c ", &c);
if (c == '?')
n++;
else if (c == '+') {
p++;
sign[s++] = 1;
} else if (c == '-') {
m++;
sign[s++] = 0;
}
}
scanf("%I64d", &num);
if (n == 1) {
printf("Possible\n%I64d = %I64d\n", num, num);
} else if (m == 0) {
if (n <= num) {
printf("Possible\n");
for (long long int i = 0; i < n - 1; i++) {
printf("1 + ");
}
printf("%I64d = %I64d\n", num - n + 1, num);
} else
printf("Impossible\n");
} else if (p <= m && p == 1)
printf("Impossible\n");
else if (p > m) {
long long int d = p - m;
if (d <= num) {
printf("Possible\n");
printf("%I64d ", num - d + 1);
for (long long int i = 0; i < n - 1; i++) {
if (sign[i])
printf("+ ");
else if (!sign[i])
printf("- ");
printf("1 ");
}
printf("= %I64d\n", num);
} else {
long long int itachi = num * m;
if (p - itachi > num)
printf("Impossible\n");
else {
printf("Possible\n");
long long int baki = 1, cnt = 0, f = 1;
if (p - itachi < num) {
while (p + (num - 1) * cnt - itachi < num) {
cnt++;
}
cnt--;
baki = itachi + num - p - ((num - 1) * (cnt)) + 1;
}
printf("%I64d ", baki);
for (long long int i = 0; i < n - 1; i++) {
if (sign[i]) {
if (cnt > 0) {
printf("+ %I64d ", num);
cnt--;
} else
printf("+ 1 ");
} else if (!sign[i]) {
printf("- %I64d ", num);
}
}
printf("= %I64d\n", num);
}
}
} else if (p <= m && p > 1) {
long long int itachi = num * p;
if (itachi - m < num)
printf("Impossible\n");
else {
printf("Possible\n");
long long int dd = num, cnt = 0, minu = (itachi - num) / m, mi = m;
long long int monu = itachi - num - (minu * (m - 1));
if (monu > num) {
dd = monu - num;
monu = num;
while (dd > num) {
dd = dd - num + 1;
cnt++;
}
dd = num - dd;
}
printf("%I64d ", dd);
for (long long int i = 0; i < n - 1; i++) {
if (sign[i]) {
if (cnt > 0) {
printf("+ 1 ");
cnt--;
} else
printf("+ %I64d ", num);
} else if (!sign[i] && mi > 1) {
printf("- %I64d ", minu);
mi--;
} else if (!sign[i] && mi == 1) {
printf("- %I64d ", monu);
}
}
printf("= %I64d\n", num);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char buf[1010];
pair<vector<int>, int> Parse() {
pair<vector<int>, int> res;
stringstream ss;
ss << buf;
string tok;
int sgn = 1;
while (ss >> tok) {
if (tok[0] == '?')
res.first.push_back(sgn);
else if (tok[0] == '+')
sgn = 1;
else if (tok[0] == '-')
sgn = -1;
else
res.second = atoi(tok.c_str());
}
return res;
}
int main() {
gets(buf);
auto expr = Parse();
long long add = count(expr.first.begin(), expr.first.end(), 1);
long long sub = count(expr.first.begin(), expr.first.end(), -1);
long long alo = add * 1, ahi = add * expr.second;
long long rlo = sub * 1, rhi = sub * expr.second;
long long lo = alo - rhi, hi = ahi - rlo;
if (expr.second < lo || expr.second > hi)
puts("Impossible");
else {
puts("Possible");
long long x, y;
long long t = expr.second;
if (rlo <= ahi - t && rhi >= ahi - t) x = ahi, y = ahi - t;
if (rlo <= alo - t && rhi >= alo - t) x = alo, y = alo - t;
if (alo <= t + rlo && ahi >= t + rlo) x = t + rlo, y = rlo;
if (alo <= t + rhi && ahi >= t + rhi) x = t + rhi, y = rhi;
vector<int> plus(add, 1);
vector<int> minus(sub, 1);
x -= add, y -= sub;
for (int i = 0; i < add; ++i) {
long long rem = min(x, t - 1);
plus[i] += rem;
x -= rem;
}
for (int i = 0; i < sub; ++i) {
long long rem = min(y, t - 1);
minus[i] += rem;
y -= rem;
}
for (int i = 0; i < ((int)(expr.first).size()); ++i) {
if (i) printf(" %c ", expr.first[i] > 0 ? '+' : '-');
if (expr.first[i] > 0) printf("%d", plus.back()), plus.pop_back();
if (expr.first[i] < 0) printf("%d", minus.back()), minus.pop_back();
}
printf(" = %d\n", expr.second);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int c;
int p[111];
int r;
int v[111];
int main() {
string x;
cin >> x;
p[1] = 1;
int c = 1;
while (true) {
string a;
cin >> a;
if (a == "+") {
cin >> a;
c++;
p[c] = 1;
} else if (a == "-") {
cin >> a;
c++;
p[c] = -1;
} else {
cin >> r;
break;
}
}
int u = 0;
for (int i = 1; i <= c; i++) {
u += p[i];
v[i] = 1;
}
if (u < r) {
for (int i = 1; i <= c; i++) {
if (p[i] != 1) continue;
int x = min(r - 1, r - u);
v[i] += x;
u += x;
}
} else if (u > r) {
for (int i = 1; i <= c; i++) {
if (p[i] != -1) continue;
int x = min(r - 1, u - r);
v[i] += x;
u -= x;
}
}
if (u == r) {
cout << "Possible\n";
cout << v[1] << " ";
for (int i = 2; i <= c; i++) {
if (p[i] == 1)
cout << "+ ";
else
cout << "- ";
cout << v[i] << " ";
}
cout << "= " << r << "\n";
} else {
cout << "Impossible\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string st;
getline(cin, st);
long long num = 0;
bool chk = false;
for (int i = 0; i < st.size(); i++) {
if (st[i] == '=') {
chk = true;
continue;
}
if (chk && st[i] >= '0' && st[i] <= '9') {
num = num * 10 + st[i] - '0';
}
}
int m = 0, p = 0;
int q = 0;
for (int i = 0; i < st.size(); i++) {
if (st[i] == '-')
m++;
else if (st[i] == '+')
p++;
else if (st[i] == '?')
q++;
}
if (p == 0 && m == 0) {
cout << "Possible" << endl;
cout << num << " = " << num << endl;
} else if (p == 0 || (m == 0 && (p >= num))) {
cout << "Impossible" << endl;
} else if (m == 0) {
cout << "Possible" << endl;
long long fs = num - p;
cout << fs;
for (int i = 1; i < st.size(); i++) {
if (st[i] == '+') cout << " + 1";
}
cout << " = " << num << endl;
} else {
if (p == m) {
cout << "Possible" << endl;
cout << num;
for (int i = 1; i < st.size(); i++) {
if (st[i] == '+' || st[i] == '-') cout << " " << st[i] << " 1";
}
cout << " = " << num << endl;
} else {
p++;
int sum = p - m - num;
if (sum == 0) {
cout << "Possible" << endl;
cout << '1';
for (int i = 1; i < st.size(); i++) {
if (st[i] == '+' || st[i] == '-') cout << " " << st[i] << " 1";
}
cout << " = " << num << endl;
} else if (sum > 0) {
int vage = sum / m, rem = sum % m;
vage++;
if (vage > num || (rem > 0 && vage + 1 > num)) {
cout << "Impossible" << endl;
} else {
cout << "Possible" << endl;
cout << '1';
for (int i = 1; i < st.size(); i++) {
if (st[i] == '+')
cout << " " << st[i] << " 1";
else if (st[i] == '-') {
if (rem > 0) {
cout << " " << st[i] << " " << vage + 1;
rem--;
} else
cout << " " << st[i] << " " << vage;
}
}
cout << " = " << num << endl;
}
} else {
sum *= (-1);
int vage = sum / p, rem = sum % p;
vage++;
if (vage > num || (rem > 0 && vage + 1 > num)) {
cout << "Impossible" << endl;
} else {
cout << "Possible" << endl;
if (rem > 0) {
cout << vage + 1;
rem--;
} else
cout << vage;
for (int i = 1; i < st.size(); i++) {
if (st[i] == '-')
cout << " " << st[i] << " 1";
else if (st[i] == '+') {
if (rem > 0) {
cout << " " << st[i] << " " << vage + 1;
rem--;
} else
cout << " " << st[i] << " " << vage;
}
}
cout << " = " << num << endl;
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int xp = 0, xn = 0, n, flag = 0;
void positive(int pos[], int sum) {
for (int i = 0; i < xp; i++) {
if ((sum + (n - 1)) < n) {
sum += (n - 1);
pos[i] = n;
} else if ((sum + (n - 1)) >= n) {
if ((n - 1 - sum) <= n) {
pos[i] += n - sum;
flag = 1;
return;
} else {
pos[i] = n;
sum += n - 1;
}
}
}
}
void negative(int neg[], int sum) {
for (int i = 0; i < xn; i++) {
if ((sum - (n - 1)) > n) {
sum -= (n - 1);
neg[i] = n;
} else if ((sum - (n - 1)) <= n) {
if ((sum - n) <= n) {
neg[i] = 0;
sum++;
neg[i] = sum - n;
flag = 1;
return;
} else {
neg[i] = 0;
sum++;
neg[i] = n;
sum -= n;
}
}
}
}
int main() {
char s[200];
for (int i = 0;; i++) {
cin >> s[i];
if (s[i] == '=') {
s[i + 1] = '\0';
break;
}
}
int sum = 0;
cin >> n;
int t = 0;
for (int i = 0; s[i] != '\0'; i++) {
t++;
}
for (int i = 0; i < t; i++) {
if (s[i] == '+') xp++;
if (s[i] == '-') xn++;
}
xp++;
int pos[xp], neg[xn];
for (int i = 0; i < xp; i++) {
pos[i] = 1;
sum++;
}
for (int i = 0; i < xn; i++) {
neg[i] = 1;
sum--;
}
if (sum == n) flag = 1;
if (sum < n) {
positive(pos, sum);
} else if (sum > n) {
negative(neg, sum);
}
if (flag == 1) {
int st = 1, nt = 0;
cout << "Possible" << endl;
cout << pos[0] << " ";
for (int i = 1; i < t; i++) {
if (s[i] == '?') {
if (s[i - 1] == '+') {
cout << pos[st] << " ";
st++;
}
if (s[i - 1] == '-') {
cout << neg[nt] << " ";
nt++;
}
} else
cout << s[i] << " ";
}
cout << n;
} else
cout << "Impossible";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long double eps = 1e-9;
const int inf = mod;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
if (a < 0) return -1;
if (a > 0) return 1;
return 0;
}
template <typename T>
inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
long long n;
string s[1000];
long long ext[2];
long long sum = 0;
long long val[1000];
int main() {
string f;
int cnt = 1;
cin >> f;
while (1) {
cin >> f;
if (f == "=") {
cin >> n;
break;
} else {
s[cnt++] = f;
cin >> f;
}
}
ext[0] = 1;
ext[1] = n;
s[0] = "+";
for (long long i = 1; i < cnt; i++) {
if (s[i] == "-") {
ext[0] -= n;
ext[1] -= 1;
} else {
ext[0] += 1;
ext[1] += n;
}
}
if (n < ext[0] || n > ext[1]) {
cout << "Impossible", exit(0);
;
}
for (long long i = 0; i < cnt; i++) {
val[i] = 1;
if (s[i] == "+")
sum++;
else
sum--;
}
for (long long i = 0; i < cnt; i++) {
if (sum > n) {
if (s[i] == "-") {
auto adjust = min(sum - n, n - val[i]);
sum -= adjust;
val[i] += adjust;
}
}
if (sum < n) {
if (s[i] == "+") {
auto adjust = min(n - sum, n - val[i]);
sum += adjust;
val[i] += adjust;
}
}
}
cout << "Possible" << endl;
cout << val[0];
for (long long i = 1; i < cnt; i++) {
cout << " " << s[i] << ' ' << val[i];
}
cout << " = " << n;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int pos_num = 0, neg_num = 0, res = 0;
void analysis(string str) {
int now = 1;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '?') {
if (now == 1)
pos_num++;
else if (now == -1)
neg_num++;
} else if (str[i] == '+')
now = 1;
else if (str[i] == '-')
now = -1;
else if (str[i] == '=') {
res = 0;
for (int j = i + 1; j < str.size(); j++) {
char c = str[j];
if ('0' <= c && c <= '9') {
res = res * 10 + c - '0';
}
}
break;
}
}
}
void work_in(string str) {
int pos_l = pos_num, pos_r = pos_num * res;
int neg_l = neg_num, neg_r = neg_num * res;
int pos = res, neg = 0;
if (pos < pos_l) {
int d = pos_l - pos;
pos += d;
neg += d;
}
if (neg < neg_l) {
int d = neg_l - neg;
pos += d;
neg += d;
}
assert(pos <= pos_r && neg <= neg_r);
cout << "Possible" << endl;
pos -= pos_num, neg -= neg_num;
int sig = 1;
for (int i = 0; i < str.size(); i++) {
if (str[i] != '?') cout << str[i];
if (str[i] == '+')
sig = 1;
else if (str[i] == '-')
sig = -1;
else if (str[i] == '?') {
if (sig == 1) {
int d = min(res - 1, pos);
cout << 1 + d;
pos -= d;
} else if (sig == -1) {
int d = min(res - 1, neg);
cout << 1 + d;
neg -= d;
}
}
}
}
const int MAXL = 1000;
char line[MAXL];
int main(void) {
cin.getline(line, MAXL);
analysis(line);
int mxres = pos_num * res - neg_num;
int mnres = pos_num - res * neg_num;
if (!(mnres <= res && res <= mxres)) {
cout << "Impossible" << endl;
} else {
work_in(line);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dim, n, i, ans, act;
int v[111];
int sg[111];
char s[111 * 30];
int read_nr() {
int ans = 0;
for (int i = 1; i <= dim; i++)
if ('0' <= s[i] && s[i] <= '9') ans = ans * 10 + s[i] - '0';
return ans;
}
int main() {
gets(s + 1);
dim = strlen(s + 1);
sg[1] = +1;
for (i = 1; i <= dim; i++) {
if (s[i] == '?') n++;
if (s[i] == '+') sg[n + 1] = +1;
if (s[i] == '-') sg[n + 1] = -1;
}
ans = read_nr();
for (i = 1; i <= n; i++) {
if (sg[i] < 0) {
v[i] = 1;
act--;
} else {
v[i] = 1;
act++;
}
}
if (act < ans) {
for (i = 1; i <= n; i++) {
if (sg[i] < 0) continue;
int addon = min(ans - act, ans - v[i]);
v[i] += addon;
act += addon;
}
} else {
for (i = 1; i <= n; i++) {
if (sg[i] > 0) continue;
int addon = min(act - ans, ans - v[i]);
v[i] += addon;
act -= addon;
}
}
if (act != ans) {
printf("Impossible");
return 0;
}
printf("Possible\n");
for (i = 1; i <= n; i++) {
if (i > 1) printf("%c ", (sg[i] < 0 ? '-' : '+'));
printf("%d ", v[i]);
}
printf("= %d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char q, op;
cin >> q;
int pos = 1, neg = 0;
vector<bool> pos_entry(1, true);
int n = -1;
while (true) {
cin >> op;
if (op == '=') {
cin >> n;
break;
} else {
cin >> q;
if (op == '+') {
pos++;
pos_entry.push_back(true);
} else {
neg++;
pos_entry.push_back(false);
}
}
}
if (pos - neg * n <= n && n <= pos * n - neg) {
cout << "Possible" << endl;
int target = n - pos + neg;
for (int i = 0; i < pos_entry.size(); i++) {
if (i > 0) cout << (pos_entry[i] ? "+ " : "- ");
int qn = 1;
if ((target < 0 && !pos_entry[i]) || (target > 0 && pos_entry[i])) {
qn = min(abs(target) + 1, n);
if (target > 0)
target -= (qn - 1);
else
target += (qn - 1);
}
cout << qn << " ";
}
cout << "= " << n << endl;
} else {
cout << "Impossible" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
const long long inf = ml * ml * ml + 7;
ifstream in;
ofstream out;
long long n, i, j, res, pl, mi;
vector<string> m;
bool viv = false;
set<string> nn;
string s;
int main() {
nn.insert("?");
nn.insert("-");
nn.insert("+");
nn.insert("=");
m.push_back("+");
s = "+";
pl++;
while (nn.find(s) != nn.end()) {
cin >> s;
if (s == "+") pl++;
if (s == "-") mi++;
m.push_back(s);
}
n = m.size() - 2;
long long r = 0;
long long u = 1;
while (m.back().size() > 0) {
r += (m.back().back() - '0') * u;
m.back().pop_back();
u *= 10;
}
long long mar = pl * r - mi;
long long mir = pl - mi * r;
if (r < mir || mar < r) {
cout << "Impossible" << endl;
return 0;
}
cout << "Possible" << endl;
long long raz = mar - r;
for (i = 0; i < n; i++) {
if (m[i] == "?") {
long long z = -1;
if (m[i - 1] == "+") z = r;
long long ub = min(raz, r - 1);
z -= ub;
raz -= ub;
cout << abs(z) << ' ';
} else if (i != 0)
cout << m[i] << ' ';
}
cout << "= " << r << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:536870912")
const int MOD = 1000000007;
const int INF = 1000000001;
const int MAXN = 100010;
const long double EPS = 1e-6;
const int HASH_POW = 29;
const long double PI = acos(-1.0);
using namespace std;
void my_return(int code) { exit(code); }
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
mt19937 mt_rand(time(0));
string s;
map<int, string> foo;
int cnt = 1, n;
cin >> s;
foo[cnt++] = "+";
while (true) {
cin >> s;
if (s != "=") {
foo[cnt++] = s;
cin >> s;
} else {
cin >> n;
break;
}
}
--cnt;
int cntplus = 0, cntminus = 0;
for (map<int, string>::iterator it = foo.begin(); it != foo.end(); ++it)
if (it->second == "+")
++cntplus;
else
++cntminus;
int ll1 = n - n * cntplus, rr1 = n - cntplus, ll2 = -n * cntminus,
rr2 = -cntminus;
if (min(rr1, rr2) < max(ll1, ll2)) {
cout << "Impossible\n";
my_return(0);
}
cout << "Possible\n";
int valminus = -max(ll1, ll2), valplus = n + valminus;
int baseminus = cntminus ? valminus / cntminus : 0,
baseplus = cntplus ? valplus / cntplus : 0;
int remminus = cntminus ? valminus % cntminus : 1,
remplus = cntplus ? valplus % cntplus : 1;
int deltaminus = 1, deltaplus = 1;
if (remminus < 0) {
remminus = -remminus;
deltaminus = -deltaminus;
}
if (remplus < 0) {
remplus = -remplus;
deltaplus = -deltaplus;
}
for (int i = 1; i <= cnt; ++i) {
if (i > 1) cout << " " << foo[i] << " ";
if (foo[i] == "+") {
if (remplus == 0) deltaplus = 0;
cout << baseplus + deltaplus;
--remplus;
} else {
if (remminus == 0) deltaminus = 0;
cout << baseminus + deltaminus;
--remminus;
}
}
cout << " = " << n << endl;
my_return(0);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char next;
long long int neg = 0, pos = 0;
cin >> next;
bool estado = false;
string orden = "";
while (next != '=') {
if (next == ' ') {
cin >> next;
}
if (next == '-') {
estado = true;
cin >> next;
}
if (next == '+') {
estado = false;
cin >> next;
}
if (next == '?') {
if (estado) {
orden += '-';
neg++;
} else {
orden += '+';
pos++;
}
}
cin >> next;
}
long long int num;
cin >> num;
long long int coefs[neg + pos];
if ((neg > ((pos - 1) * num)) || ((pos - neg * num) > num)) {
cout << "Impossible";
return 0;
}
long long int valpos = 0, valneg = 0;
cout << "Possible" << endl;
if (neg == 0) {
long long int cv = trunc(num / pos);
long long int sum = 0;
for (int i = 0; i < pos; i++) {
coefs[i] = cv;
}
sum = cv * pos - num;
if (sum != 0) {
for (int i = 0; i < pos; i++) {
coefs[i]++;
sum++;
if (sum == 0) break;
}
}
cout << coefs[0];
for (int i = 1; i < pos; i++) {
cout << " + " << coefs[i];
}
cout << " = " << num;
return 0;
}
if (pos - 1 > neg) {
valpos = 1;
valneg = trunc((pos - 1) / neg);
} else {
valneg = 1;
valpos = trunc(neg / (pos - 1));
}
int ref;
for (int i = 0; i < orden.size(); i++) {
if (orden[i] == '-') {
coefs[i] = -valneg;
} else
coefs[i] = valpos;
}
coefs[0] = num;
long long int suma = 0;
for (int i = 0; i < neg + pos; i++) {
suma += coefs[i];
}
suma -= num;
if (suma < 0) {
for (int i = 1; i < neg + pos; i++) {
if (coefs[i] > 0 && (abs(coefs[i]) < num)) {
coefs[i]++;
suma++;
}
if (suma == 0) break;
}
}
if (suma > 0) {
for (int i = 1; i < neg + pos; i++) {
if (coefs[i] < 0 && (abs(coefs[i]) < num)) {
coefs[i]--;
suma--;
}
if (suma == 0) break;
}
}
for (int i = 1; i < neg + pos; i++) {
if (abs(coefs[i]) > num) {
if (coefs[i] < 0) {
suma += -num - coefs[i];
coefs[i] = -num;
} else {
suma += num - coefs[i];
coefs[i] = num;
}
}
}
if (suma > 0) {
coefs[0] -= suma;
suma -= suma;
}
if (suma < 0) {
coefs[0] += suma;
suma -= suma;
}
cout << coefs[0];
for (int i = 1; i < pos + neg; i++) {
if (coefs[i] < 0)
cout << " - " << abs(coefs[i]);
else
cout << " + " << coefs[i];
}
cout << " = " << suma + num;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<char> s;
int p = 1, m = 0;
s.push_back('+');
for (int i = 0;; ++i) {
char c;
cin >> c >> c;
s.push_back(c);
if (c == '=') break;
if (c == '+')
++p;
else
++m;
}
s.pop_back();
cin >> n;
if (p * n - m >= n && n >= p - m * n)
cout << "Possible" << endl;
else {
cout << "Impossible" << endl;
return 0;
}
int k = (int)(s).size();
vector<int> a(k);
for (int i = 0; i < k; ++i) a[i] = 1;
if (p - m >= n) {
int t = p - m - n;
for (int i = 0; i < k; ++i) {
if (s[i] == '-') {
a[i] += min(t, n - 1);
t -= min(t, n - 1);
}
}
} else {
int t = n - p + m;
for (int i = 0; i < k; ++i) {
if (s[i] == '+') {
a[i] += min(t, n - 1);
t -= min(t, n - 1);
}
}
}
cout << a[0] << " ";
for (int i = 1; i < k; ++i) cout << s[i] << " " << a[i] << " ";
cout << "= " << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
char s[112345];
int main() {
int i, j, n;
s[0] = '+';
s[1] = ' ';
fgets(s + 2, (sizeof s) - 2, stdin);
int sz = strlen(s) - 1;
for (i = sz - 2; isdigit(s[i]); i--)
;
n = atoi(s + i + 1);
int cp = 0, cm = 0;
for (i = 0; i < sz; i++)
if (s[i] == '+')
cp++;
else if (s[i] == '-')
cm++;
int mn = cp - n * cm, mx = n * cp - cm;
if (n < mn || n > mx) {
puts("Impossible");
return 0;
}
puts("Possible");
for (i = 0; i < sz; i++)
if (s[i] == '+') {
if (i) printf(" + ");
int v = n;
while (mx > n && v > 1) v--, mx--;
printf("%d", v);
} else if (s[i] == '-') {
printf(" - ");
int v = 1;
while (mx > n && v < n) v++, mx--;
printf("%d", v);
}
printf(" = %d\n", n);
}
|
#include <bits/stdc++.h>
using namespace std;
long long sigma(long long n) { return (n * (n + 1) / 2); }
long long MOD(long long x) {
if (x >= 0) return x;
return (-x);
}
bool great(long long a, long long b) { return a > b; }
bool p_sm(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first == b.first) {
return (a.second < b.second);
}
return (a.first < b.first);
}
long long max3(long long a, long long b, long long c) {
return (max(a, max(b, c)));
}
long long max4(long long a, long long b, long long c, long long d) {
return (max(max(a, b), max(c, d)));
}
long long min3(long long a, long long b, long long c) {
return (min(a, min(b, c)));
}
long long min4(long long a, long long b, long long c, long long d) {
return (min(min(a, b), min(c, d)));
}
vector<long long> factors(long long x) {
vector<long long> v;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
v.push_back(i);
if (i != x / i) {
v.push_back(x / i);
}
}
}
return v;
}
vector<long long> ispr;
vector<long long> min_prime;
unordered_map<long long, long long> prime_fact(long long x) {
unordered_map<long long, long long> mp;
while (x != 1) {
mp[min_prime[x]]++;
x /= min_prime[x];
}
return mp;
}
map<long long, long long> factorizee(long long n) {
map<long long, long long> mp;
for (long long i = 2; i * i <= n; ++i) {
while (n % i == 0) {
mp[i]++;
n /= i;
}
}
if (n != 1) {
mp[n]++;
}
return mp;
}
long long power(long long a, long long b, long long m) {
long long ans = 1;
while (b > 0) {
if (b % 2) {
ans = (ans * a) % m;
}
a = (a * a) % m;
b = b / 2;
}
return ans;
}
int main() {
string s;
getline(cin, s);
long long xx;
string ss = "";
long long po, ne, to;
po = ne = to = 0;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == ' ' || s[i] == '+') {
continue;
} else if (s[i] == '?') {
to++;
} else if (s[i] == '-') {
ne++;
} else if (s[i] != '=') {
for (long long j = i; j < s.size(); j++) {
ss += s[j];
}
break;
}
}
po = to - ne;
xx = stoll(ss);
long long jj = xx + ne;
if ((po * xx) < jj) {
cout << "Impossible" << endl;
exit(0);
;
}
long long n[ne];
for (int i = 0; i < ne; i++) {
n[i] = 0;
};
long long ns = -1;
if (po > jj) {
for (long long i = 1; i < 100000004; i++) {
if (ne < i) {
if ((ne * xx) >= i) {
if ((i + xx) <= (xx * po)) {
if ((xx * po) <= ((i + xx) * xx)) {
ns = i;
}
}
}
}
}
if (ns == -1) {
cout << "Impossible" << endl;
exit(0);
;
}
long long ii = 0;
long long i = 0;
while (ii < ns) {
n[i++]++;
ii++;
i %= ne;
}
} else {
ns = ne;
for (long long i = 0; i < ne; i++) {
n[i] = 1;
}
}
long long p[po];
for (int i = 0; i < po; i++) {
p[i] = 0;
};
long long ii = 0;
long long i = 0;
jj = xx + ns;
while (ii < jj) {
p[i++]++;
i %= po;
ii++;
}
long long ju = 0;
char lc = '+';
long long jk = 0;
cout << "Possible" << endl;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == '?') {
if (lc == '+') {
cout << p[ju++];
} else {
cout << n[jk++];
}
} else {
cout << s[i];
}
if (s[i] == '+' || s[i] == '-') {
lc = s[i];
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000000 + 7;
int addm(int& a, int b) { return (a += b) < mod ? a : a -= mod; }
template <class T, class U>
bool smin(T& a, U b) {
return a > b ? (a = b, 1) : 0;
}
template <class T, class U>
bool smax(T& a, U b) {
return a < b ? (a = b, 1) : 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<char> sgns{'+'};
int n;
char cc;
cin >> cc;
long long st = 1;
while (cin >> cc && cc != '=') {
sgns.push_back(cc);
if (cc == '-')
st--;
else
st++;
cin >> cc;
}
cin >> n;
vector<int> sol(sgns.size(), 1);
for (int i = 0; i < sgns.size(); i++) {
if (st > n && sgns[i] == '-') {
if (st - n > n - 1) {
sol[i] = n;
st -= n - 1;
} else {
sol[i] += st - n;
st = n;
}
}
if (st < n && sgns[i] == '+') {
if (n - st > n - 1) {
sol[i] = n;
st += n - 1;
} else {
sol[i] += n - st;
st = n;
}
}
}
if (st == n) {
cout << "Possible\n";
cout << sol[0];
for (int i = 1; i < sgns.size(); i++) {
cout << ' ' << sgns[i] << ' ' << sol[i];
}
cout << " = " << n << endl;
} else
cout << "Impossible\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char curr;
vector<int> p;
int ms = 0, ps = 0, n, curr_sum;
p.push_back(1);
ps++;
while (curr != '=') {
cin >> curr;
if ((curr == ' ') || (curr == '?')) continue;
if (curr == '-') {
p.push_back(-1);
ms++;
}
if (curr == '+') {
p.push_back(1);
ps++;
}
}
cin >> n;
curr_sum = ps * n - ms;
if (curr_sum < n) {
cout << "Impossible" << endl;
return 0;
}
curr_sum = ps - ms * n;
if (curr_sum > n) {
cout << "Impossible" << endl;
return 0;
}
vector<int> ans(p.size());
curr_sum = 0;
for (int i = 0; i < p.size(); ++i) {
if (p[i] == 1)
ans[i] = n;
else
ans[i] = -1;
curr_sum += ans[i];
}
int i = 0;
while (curr_sum != n) {
if (p[i] == -1) {
if (ans[i] > (-n)) {
ans[i]--;
curr_sum--;
} else {
++i;
continue;
}
} else {
if (ans[i] > (1)) {
ans[i]--;
curr_sum--;
} else {
++i;
continue;
}
}
}
cout << "Possible" << endl;
cout << ans[0] << " ";
for (int i = 1; i < p.size(); ++i) {
if (p[i] == 1)
cout << "+ ";
else
cout << "- ";
cout << abs(ans[i]) << " ";
}
cout << "= " << n;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[110];
int main() {
int i, n, len, p, j;
string s;
getline(cin, s);
len = 0;
p = 0;
a[len++] = 1;
p++;
for (i = 0; i < s.length(); i++) {
if (s[i] == '+') {
a[len++] = 1;
p++;
} else if (s[i] == '-') {
a[len++] = -1;
p--;
} else if (s[i] == '=') {
n = 0;
for (j = i + 2; j < s.length(); j++) {
n = n * 10 + (s[j] - '0');
}
break;
}
}
for (i = 0; i < len; i++) {
while (p > n && a[i] < 0 && abs(a[i]) < n) {
p--;
a[i]--;
}
while (p < n && a[i] > 0 && a[i] < n) {
p++;
a[i]++;
}
}
if (p != n) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
for (i = 0; i < len; i++) {
if (i > 0) {
if (a[i] > 0)
printf(" + ");
else
printf(" - ");
}
printf("%d", abs(a[i]));
}
printf(" = %d\n", n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int positive = -1, negative = -1;
int pluses = 1, minuses;
int n;
bool can_divide(int N, int x) {
if (N / x < 1) return 0;
return (N / x) + (N % x > 0) >= 1 && (N / x) + (N % x > 0) <= n;
}
int main() {
string s;
getline(cin, s);
n = s.length();
int eq = 0;
bool test = 0;
for (int i = 0; i < n; ++i)
if (s[i] == '+')
pluses++;
else if (s[i] == '-')
minuses++;
else if (s[i] == '=')
eq = i;
else if (s[i] == '?')
test = 1;
if (!test) {
cout << "Possible\n";
cout << s;
return 0;
}
int tmp = 0;
eq += 2;
while (eq < n) {
tmp = tmp * 10 + (s[eq] - '0');
++eq;
}
n = tmp;
if (n == 1 && !(pluses - minuses == 1)) {
cout << "Impossible";
return 0;
}
for (int i = 0; i <= n * 50; ++i) {
if (pluses >= minuses) {
int neg = i;
int pos = i + n;
if (minuses == 0 && neg > 0) continue;
if ((minuses == 0 || can_divide(neg, minuses)) &&
can_divide(pos, pluses)) {
negative = neg, positive = pos;
break;
}
} else {
int neg = i - n;
int pos = i;
if (minuses == 0 && neg > 0) continue;
if (neg >= 1 && (minuses == 0 || can_divide(neg, minuses)) &&
can_divide(pos, pluses)) {
negative = neg, positive = pos;
break;
}
}
}
if (positive == -1) {
cout << "Impossible";
return 0;
}
cout << "Possible\n";
cout << (positive / pluses) + (positive % pluses > 0);
if (positive % pluses) --positive;
for (int i = 1; i < (int)s.length(); ++i) {
if (s[i] != '?')
cout << s[i];
else {
if (s[i - 2] == '+') {
cout << (positive / pluses) + (positive % pluses > 0);
if (positive % pluses) --positive;
} else {
cout << (negative / minuses) + (negative % minuses > 0);
if (negative % minuses) --negative;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string str;
int n = 0;
getline(cin, str);
int at;
for (at = str.size() - 1; str[at] != ' '; at--)
;
for (int i = at + 1; i < str.size(); i++) n = (n * 10) + (str[i] - '0');
int num = 0, neg = 0, pos;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '?') num++;
if (str[i] == '-') neg++;
}
pos = num - neg;
if (pos * n - neg < n || pos - neg * n > n)
cout << "Impossible\n";
else {
cout << "Possible\n";
char ch = '+';
int target = n + neg;
int cont = 0;
int fim = target % pos;
int rest = target / pos;
if (rest == 0) {
target = pos - n;
fim = target % neg;
rest = target / neg;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '?') {
if (ch == '+')
cout << '1';
else if (cont < fim) {
cout << rest + 1;
cont++;
} else
cout << rest;
} else {
cout << str[i];
if (str[i] == '-') ch = '-';
if (str[i] == '+') ch = '+';
}
}
} else {
for (int i = 0; i < str.size(); i++) {
if (str[i] == '?') {
if (ch == '-')
cout << '1';
else if (cont < fim) {
cout << rest + 1;
cont++;
} else
cout << rest;
} else {
cout << str[i];
if (str[i] == '-') ch = '-';
if (str[i] == '+') ch = '+';
}
}
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v) os << sep << x, sep = ", ";
return os << '}';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string str;
getline(cin, str);
istringstream in(str);
vector<int> positive, negative;
positive.push_back(0);
in >> str;
string token;
int N;
int index = 1;
while (in >> token) {
if (token == "=") {
in >> N;
break;
}
string _;
in >> _;
if (token == "+")
positive.push_back(index);
else
negative.push_back(index);
index++;
}
int A = int(positive.size());
int B = int(negative.size());
int minimum = A * 1 - B * N;
int maximum = A * N - B * 1;
if (!(minimum <= N && N <= maximum)) {
cout << "Impossible" << '\n';
return 0;
}
vector<int> p_values(A, 1);
vector<int> n_values(B, N);
int current = minimum;
for (int i = 0; i < A; i++) {
int add = min(N - current, N - 1);
p_values[i] += add;
current += add;
}
for (int i = 0; i < B; i++) {
int add = min(N - current, N - 1);
n_values[i] -= add;
current += add;
}
cout << "Possible" << '\n';
vector<int> answer(A + B, -1);
for (int i = 0; i < A; i++) answer[positive[i]] = p_values[i];
vector<bool> is_negative(A + B, false);
for (int i = 0; i < B; i++) {
answer[negative[i]] = n_values[i];
is_negative[negative[i]] = true;
}
for (int i = 0; i < A + B; i++) {
if (i > 0) {
cout << ' ' << (is_negative[i] ? '-' : '+') << ' ';
}
cout << answer[i];
}
cout << " = " << N << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int l, r, aand, diver, n, sum, jia[105], jian[105], counnt;
char ch[3], fuhao[105];
int main() {
while (~scanf("%s", ch)) {
if (ch[0] == '?')
continue;
else if (ch[0] == '+') {
aand++;
fuhao[counnt++] = '+';
} else if (ch[0] == '-') {
diver++;
fuhao[counnt++] = '-';
} else
break;
}
sum = aand + 1 - diver;
for (int i = 0; i <= aand; i++) {
jia[i] = 1;
}
for (int i = 0; i < diver; i++) {
jian[i] = 1;
}
scanf("%d", &n);
l = aand + 1 - diver * n;
r = (aand + 1) * n - diver;
int ant1 = 0, ant2 = 0;
if (n < l || n > r) {
printf("Impossible\n");
} else {
printf("Possible\n");
while (sum != n) {
if (sum < n) {
while (jia[ant1] < n && sum != n) {
jia[ant1]++;
sum++;
}
ant1++;
} else {
while (jian[ant2] < n && sum != n) {
jian[ant2]++;
sum--;
}
ant2++;
}
}
int res1 = 0, res2 = 0;
printf("%d", jia[res1++]);
for (int i = 0; i < aand + diver; i++) {
if (fuhao[i] == '+') {
printf(" + %d", jia[res1++]);
} else {
printf(" - %d", jian[res2++]);
}
}
printf(" = %d\n", n);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[103];
int main() {
int neg = 0, pos = 1, sum = 0, k = 0, n;
while (1) {
char c;
scanf(" %c %c", &c, &c);
if (c == '=') break;
if (c == '-')
neg++;
else
pos++;
s[k++] = c;
}
scanf("%d", &n);
if (n < pos - neg * n || n > pos * n - neg)
printf("Impossible\n");
else {
printf("Possible\n");
for (int i = 0; i < k; i++) {
int flag = 1;
if (i > 0 && s[i - 1] == '-') flag = -1;
if (flag == 1)
pos--;
else
neg--;
for (int j = 1; j <= n; j++) {
if (sum + j * flag + pos - neg * n <= n &&
n <= sum + j * flag + pos * n - neg) {
printf("%d %c ", j, s[i]);
sum += flag * j;
break;
}
}
}
printf("%d = %d\n", abs(n - sum), n);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[100000];
int i, j, x = 0, n, cp = 0, cm = 0, tmp, ap, am, pkp, flag = 0, p[10000],
pp = 0, m[10000], mm = 0, mmm = 0, nnn = 0, a;
scanf(" %[^\n]", s);
n = strlen(s);
for (i = 0; i < n; i++) {
if (s[i] == '+')
cp++;
else if (s[i] == '-')
cm++;
if (s[i] >= 48 && s[i] <= 57) {
tmp = s[i] - 48;
x = x * 10 + tmp;
}
}
cp++;
a = min(cp, cm);
if (cp > cm) {
ap = x * (a + 1);
am = x * a;
for (i = 0; i < n; i++) {
if ((s[i] == '?' && s[i - 2] == '+') || (i == 0)) {
pkp = ap / cp;
if (pkp <= 0 || pkp > x) {
printf("Impossible\n");
flag = 1;
break;
} else {
p[pp] = pkp;
pp++;
}
ap -= pkp;
cp--;
} else if (s[i] == '?' && s[i - 2] == '-') {
pkp = am / cm;
if (pkp <= 0 || pkp > x) {
printf("Impossible\n");
flag = 1;
break;
} else {
m[mm] = pkp;
mm++;
}
am -= pkp;
cm--;
}
}
} else if (cp <= cm) {
ap = x * a;
am = x * (a - 1);
for (i = 0; i < n; i++) {
if ((s[i] == '?' && s[i - 2] == '+') || (i == 0)) {
pkp = ap / cp;
if (pkp <= 0 || pkp > x) {
printf("Impossible\n");
flag = 1;
break;
} else {
p[pp] = pkp;
pp++;
}
ap -= pkp;
cp--;
} else if (s[i] == '?' && s[i - 2] == '-') {
pkp = am / cm;
if (pkp <= 0 || pkp > x) {
printf("Impossible\n");
flag = 1;
break;
} else {
m[mm] = pkp;
mm++;
}
am -= pkp;
cm--;
}
}
}
if (flag != 1) {
printf("Possible\n");
for (i = 0; i < n; i++) {
if ((s[i] == '?' && s[i - 2] == '+') || (i == 0)) {
printf("%d", p[mmm]);
mmm++;
} else if (s[i] == '?' && s[i - 2] == '-') {
printf("%d", m[nnn]);
nnn++;
} else
printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char s[10];
int a[110], tot;
int goal, mi, pov;
int main() {
do {
scanf("%s", s);
scanf("%s", s);
tot++;
if (s[0] == '-') {
mi++;
a[tot] = -1;
} else if (s[0] == '+') {
a[tot] = 1;
}
} while (s[0] != '=');
int mic = 1;
scanf("%d", &goal);
pov = tot - mi;
int asum = max(pov, mi + goal);
int bsum = asum - goal;
int a1 = asum / pov, a2 = a1 + (asum % pov != 0);
int anum = 0, bnum = 0;
anum = asum % pov;
if (mi > 0) bnum = bsum % mi;
int b1 = 1, b2 = 1;
if (mi > 0) b1 = bsum / mi, b2 = b1 + (bsum % mi != 0);
if (a1 <= 0 || b1 <= 0 || a2 > goal || b2 > goal || (mi == 0 && pov > goal)) {
puts("Impossible");
} else {
puts("Possible");
if (anum) {
anum--;
printf("%d ", a2);
} else {
printf("%d ", a1);
}
for (int i = 1; i < tot; ++i) {
if (a[i] == 1) {
if (anum) {
anum--;
printf("+ %d ", a2);
} else {
printf("+ %d ", a1);
}
} else {
if (bnum) {
bnum--;
printf("- %d ", b2);
} else {
printf("- %d ", b1);
}
}
}
printf("= %d\n", goal);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char ch, t;
int p = 0;
int A[2];
A[0] = A[1] = 0;
int n;
vector<char> B;
while (1) {
scanf("%c%c", &ch, &t);
if (ch == '=') {
scanf("%d", &n);
break;
} else if (ch == '?')
A[p]++;
else if (ch == '+') {
p = 0;
B.push_back(ch);
} else if (ch == '-') {
p = 1;
B.push_back(ch);
}
}
int a = A[0] - A[1];
int d = (n - a) / A[0];
int temp = n - a;
a = ceil((1.0 * (n - a)) / A[0]);
if ((a < n) && (A[0] - A[1] <= n)) {
printf("Possible\n");
int Q[2];
Q[1] = Q[0] = d + 1;
int tt = (temp) - (d * A[0]);
d++;
int z = B.size();
if (tt) {
printf("%d ", d + 1);
tt--;
} else
printf("%d ", d);
for (int i = 0; i < z; i++) {
printf("%c ", B[i]);
if (B[i] == '-')
printf("1 ");
else {
if (tt) {
printf("%d ", d + 1);
tt--;
} else
printf("%d ", d);
}
}
printf("= %d\n", n);
} else if (A[0] > A[1] && A[1]) {
int x = A[0] - A[1];
int m = (x - n) / A[1];
int r = ((x - n) % A[1]) ? 1 : 0;
if (r + m < n) {
printf("Possible\n");
m++;
r = (x - n) % A[1];
int z = B.size();
printf("%d ", 1);
for (int i = 0; i < z; i++) {
printf("%c ", B[i]);
if (B[i] == '+')
printf("1 ");
else {
if (r) {
printf("%d ", m + 1);
r--;
} else
printf("%d ", m);
}
}
printf("= %d\n", n);
} else
printf("Impossible\n");
} else
printf("Impossible\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int c, d, tmp, plus, kurang, nilai, a, b, k, n = 0, min = 0, sum = 1, cek,
simpan[100], cetak[100];
char s;
simpan[0] = 1;
while (s != '=') {
scanf("%c", &s);
if (s == '?') n++;
if (s == '-') {
min++;
simpan[n] = 0;
}
if (s == '+') {
sum++;
simpan[n] = 1;
}
}
scanf("%d", &cek);
if (sum != 0) {
a = (cek + min) / sum;
b = (cek + min) % sum;
} else {
a = 0;
b = 0;
}
if (min != 0) {
c = (sum - cek) / min;
d = (sum - cek) % min;
} else {
c = 0;
d = 0;
}
if ((b > 0 && a + 1 > cek) || (b == 0 && a > cek) || (a == 0 && c > cek) ||
(c == 0 && a > cek) || (d > 0 && c + 1 > cek) || (d == 0 && c > cek) ||
(a == 0 && c == 0)) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
if (a > 0) {
for (tmp = 0; tmp < n; tmp++) {
if (simpan[tmp] == 1) {
if (b > 0) {
cetak[tmp] = a + 1;
b--;
} else {
cetak[tmp] = a;
}
} else {
cetak[tmp] = 1;
}
}
} else if (c > 0) {
for (tmp = 0; tmp < n; tmp++) {
if (simpan[tmp] == 0) {
if (d > 0) {
cetak[tmp] = c + 1;
d--;
} else {
cetak[tmp] = c;
}
} else {
cetak[tmp] = 1;
}
}
}
printf("%d ", cetak[0]);
for (tmp = 1; tmp < n; tmp++) {
if (simpan[tmp] == 1)
printf("+ ");
else
printf("- ");
printf("%d ", cetak[tmp]);
}
printf("= %d\n", cek);
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int INF = 2e9 + 7;
const int MOD = 1e9 + 7;
const long long LINF = 1e17 + 7;
const double Pi = acos(-1.);
const double EPS = 1e-8;
int n, x, y;
vector<int> p;
char nxtChar() {
char ch = getchar();
while (!(ch == '?' || ch == '+' || ch == '-' || ch == '=')) ch = getchar();
return ch;
}
int main() {
x = 1, y = 0;
char ch = nxtChar();
while (ch != '=') {
if (ch == '+') {
++x;
p.push_back(1);
} else if (ch == '-') {
++y;
p.push_back(-1);
}
ch = nxtChar();
}
scanf("%d", &n);
if (x * n < n + y || y * n + n < x) {
puts("Impossible");
} else {
puts("Possible");
int v = max(x, n + y), xx = v % x, yy = (y > 0 ? (v - n) % y : 0);
printf("%d", v / x);
for (int i = 0; i < ((int)(p.size())); ++i)
if (p[i] > 0) {
printf(" + ");
xx > 0 ? printf("%d", v / x + 1), --xx : printf("%d", v / x);
} else {
printf(" - ");
yy > 0 ? printf("%d", (v - n) / y + 1),
--yy : printf("%d", (v - n) / y);
}
printf(" = %d\n", n);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, l, x, sum = 0, n;
char ch;
vector<char> sign;
vector<int> q;
int pos = 1;
int neg = 0;
int max_possible;
int min_possible;
sign.push_back(1);
while (1) {
scanf("%c", &ch);
if (ch == '+') {
pos++;
sign.push_back(1);
} else if (ch == '-') {
neg++;
sign.push_back(-1);
} else if (ch == '=')
break;
else if (ch == '?')
q.push_back(2);
}
scanf("%d", &n);
int flag = 0;
for (i = 0; i < q.size(); i++) {
flag = 0;
if (sign[i] == 1)
pos--;
else
neg--;
max_possible = pos * n - neg * 1;
min_possible = pos * 1 - neg * n;
for (x = 1; x <= n; x++) {
if ((n - sum - x * sign[i]) <= max_possible &&
(n - sum - x * sign[i]) >= min_possible) {
flag = 1;
break;
}
}
if (flag == 0) break;
sum += x * sign[i];
q[i] = x;
}
if (flag == 1) {
cout << "Possible" << endl;
k = 1;
for (i = 0; i < q.size(); i++) {
if (sign[k] == 1)
ch = '+';
else
ch = '-';
if (i != q.size() - 1)
cout << q[i] << " " << ch << " ";
else
cout << q[i] << " ";
k++;
}
cout << '=' << " " << n << endl;
} else
cout << "Impossible" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, t;
char s[1100000];
long long st[100000l], l = 0;
long long sum[2] = {0};
int main() {
gets(s);
if (s[0] == '-') {
sum[1] = 1;
st[l++] = -1;
} else {
sum[0] = 1;
st[l++] = 1;
}
long long pre = 0;
for (int i = 1; s[i]; i++) {
if (s[i] == '+') {
st[l++] = 1;
sum[0]++;
} else if (s[i] == '-') {
st[l++] = -1;
sum[1]++;
}
if (s[i] >= '0' && s[i] <= '9') {
while (s[i] >= '0' && s[i] <= '9') {
pre *= 10;
pre += (s[i] - '0');
i++;
}
break;
}
}
if (sum[0] * pre - sum[1] >= pre && sum[0] - sum[1] * pre <= pre) {
printf("Possible\n");
long long ansA, ansB, am;
for (int i = 1; i <= pre; i++) {
long long PRE = sum[1] * i + pre;
long long pre1 = PRE / sum[0];
long long pre2 = PRE / sum[0] + !!(PRE % sum[0]);
if (pre2 >= 1 && pre2 <= pre && pre1 >= 1 && pre1 <= pre) {
ansA = pre1;
ansB = i;
break;
}
}
long long cnt = 0;
for (int i = 0; i < l; i++) {
if (st[i] > 0) {
st[i] *= ansA;
cnt += st[i];
} else
st[i] *= ansB;
}
cnt = sum[1] * ansB + pre - sum[0] * ansA;
for (int i = 0; i < l; i++) {
if (st[i] > 0) {
if (cnt > 0) {
st[i]++;
cnt--;
}
}
}
printf("%I64d", st[0]);
for (int i = 1; i < l; i++) {
printf(" ");
if (st[i] >= 0)
printf("+");
else
printf("-");
printf(" ");
printf("%I64d", abs(st[i]));
}
printf(" = %I64d\n", pre);
} else
printf("Impossible\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
vector<int> process;
cin >> a;
process.push_back(1);
cin >> a;
while (a != "=") {
string b;
cin >> b;
if (a == "+")
process.push_back(1);
else
process.push_back(-1);
cin >> a;
}
long long int n;
cin >> n;
long long int sum = 0;
long long int temp[process.size()];
for (int i = 0; i < process.size(); i++) {
if (process[i] == 1) {
temp[i] = n;
sum += n;
} else {
temp[i] = -1;
sum -= 1;
}
}
if (sum < n) {
cout << "Impossible\n";
return 0;
}
int index = 0;
while (index < process.size() && sum > n) {
while (index < process.size() && temp[index] == -1) index++;
if (index == process.size()) break;
long long int diff = min(sum - n, temp[index] - 1);
sum -= diff;
temp[index] -= diff;
index++;
}
index = 0;
while (index < process.size() && sum > n) {
while (index < process.size() && temp[index] > 0) index++;
if (index == process.size()) break;
long long int diff = min(sum - n, n - (-temp[index]));
sum -= diff;
temp[index] -= diff;
index++;
}
if (sum > n) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
cout << temp[0] << " ";
for (int i = 1; i < process.size(); i++) {
if (temp[i] < 0)
cout << "- " << (-temp[i]) << " ";
else
cout << "+ " << temp[i] << " ";
}
cout << "= " << n;
cout << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
int pos = 1, neg = 0;
string s = "+";
while (scanf("%c", &c) && c != '=') {
if (c == '+')
pos += 1, s += '+';
else if (c == '-') {
neg += 1, s += '-';
}
}
int n;
cin >> n;
if (n * pos - neg < n || pos - neg * n > n)
cout << "Impossible";
else {
bool flag = false;
int total = n * pos - neg - n;
int vpos[pos], vneg[neg];
for (int i = 0; i < pos; i++) {
vpos[i] = n;
}
for (int i = 0; i < neg; i++) {
vneg[i] = 1;
}
for (int i = 0; i < pos; i++) {
if (total - (n - 1) >= 0)
vpos[i] = 1, total -= (n - 1);
else {
vpos[i] = n - total;
flag = true;
break;
}
}
if (flag == false) {
for (int i = 0; i < neg; i++) {
if (total - (n - 1) >= 0)
vneg[i] = n, total -= (n - 1);
else {
vneg[i] = total + 1;
total = 0;
break;
}
}
}
int ps = 0;
int ns = 0;
bool flag1 = false;
cout << "Possible" << endl;
for (int i = 0; i < neg + pos; i++) {
if (s[i] == '+') {
if (flag1 == true) cout << "+ " << vpos[ps] << " ", ps += 1;
if (flag1 == false) {
cout << vpos[ps] << " ", ps += 1;
flag1 = true;
}
}
if (s[i] == '-') cout << "- " << vneg[ns] << " ", ns += 1;
}
cout << "= " << n;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, cur, s, pos, neg;
char ch, p;
queue<int> positive, negative;
vector<char> sign;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> ch;
p = ch;
while (1) {
if (p == '=') {
cin >> n;
break;
} else {
cin >> ch;
if (p == '+') {
pos++;
sign.push_back('+');
} else if (p == '-') {
neg++;
sign.push_back('-');
}
p = ch;
}
}
int left = (n + neg);
pos++;
for (int i = 0; i < pos; i++) {
positive.push(max(1, min(n, left - (pos - i) + 1)));
cur += max(1, min(n, left - (pos - i) + 1));
left = (n + neg) - cur;
}
left = cur - n;
for (int i = 0; i < neg; i++) {
negative.push(max(1, min(n, left - (neg - i) + 1)));
cur -= max(1, min(n, left - (neg - i) + 1));
left = cur - n;
}
if (cur != n) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
cout << positive.front();
positive.pop();
for (int i = 0; i < sign.size(); i++) {
cout << " " << sign[i] << " ";
if (sign[i] == '+') {
cout << positive.front();
positive.pop();
} else {
cout << negative.front();
negative.pop();
}
}
cout << " = " << n << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char c[105][2];
void solve() {
int n, cnt, add, sub;
sub = 0;
add = cnt = 1;
strcpy(c[0], "+");
while (scanf("%s", c[cnt]) && strcmp(c[cnt], "=")) {
if (strcmp(c[cnt], "?") == 0) continue;
if (strcmp(c[cnt], "+") == 0)
add++;
else
sub++;
cnt++;
}
scanf("%d", &n);
int tp = n - add + sub;
if (tp > 0 && add * (n - 1) < tp || tp < 0 && sub * (n - 1) < -tp)
puts("Impossible");
else {
puts("Possible");
for (int i = 0; i < cnt; ++i) {
int x = 1;
if (i) printf("%s ", c[i]);
if (tp > 0 && strcmp(c[i], "+") == 0) {
if (tp >= n - 1)
x = n;
else
x = x + tp;
tp -= x - 1;
} else if (tp < 0 && strcmp(c[i], "-") == 0) {
if (tp <= 1 - n)
x = n;
else
x = x - tp;
tp -= -x + 1;
}
printf("%d ", x);
}
printf("= %d\n", n);
}
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char arr[300];
int n;
int main() {
int cnt = 0;
int mx = 0, mn = 0;
for (; true; cnt++) {
cin >> arr[cnt];
if (arr[cnt] == '=') {
cin >> n;
mx = n;
mn = 1;
break;
}
}
for (int i = 1; i < cnt; i += 2) {
if (arr[i] == '+') {
mx += n;
mn += 1;
} else {
mx -= 1;
mn -= n;
}
}
if (mx >= n && mn <= n) {
cout << "Possible\n";
int diffMx = mx - n;
cout << max(1, n - diffMx) << ' ';
diffMx = max(diffMx - n + 1, 0);
for (int i = 1; i < cnt; i += 2) {
cout << arr[i] << ' ';
if (arr[i] == '+') {
cout << max(1, n - diffMx) << ' ';
diffMx = max(diffMx - n + 1, 0);
} else {
cout << min(1 + diffMx, n) << ' ';
diffMx = max(0, diffMx - n + 1);
}
}
cout << "= " << n << endl;
} else {
cout << "Impossible\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int power(int a) {
int prod = 1;
while (a--) prod *= 10;
return prod;
}
int main() {
char ch[500];
gets(ch);
int ln = strlen(ch);
vector<char> v;
v.push_back('+');
int i, num = 0;
for (i = 0; i < ln; i++) {
if (ch[i] == ' ' || ch[i] == '?')
continue;
else if (ch[i] == '=') {
i += 2;
int digit = ln - i;
int flag = 1;
for (; i < ln; i++) num += (ch[i] - '0') * power(digit - (flag++));
} else
v.push_back(ch[i]);
}
int sum = 0, mn, mx;
int sz = v.size();
for (int i = 0; i < sz; i++) {
if (v[i] == '+')
sum += 1;
else
sum -= num;
}
mn = sum;
sum = 0;
for (int i = 0; i < sz; i++) {
if (v[i] == '+')
sum += num;
else
sum -= 1;
}
mx = sum;
if (mn <= num && mx >= num) {
cout << "Possible\n";
vector<int> v1;
for (int i = 0; i < sz; i++) {
if (v[i] == '+')
v1.push_back(1);
else
v1.push_back(num);
}
int barabo = num - mn;
for (int i = 0; i < sz; i++) {
if (barabo == 0) break;
if (v[i] == '+') {
int hmm = v1[i];
if (num - v1[i] < barabo) {
barabo -= num - hmm;
v1[i] = num;
} else {
v1[i] += barabo;
barabo = 0;
break;
}
}
}
for (int i = 0; i < sz; i++) {
if (barabo == 0) break;
if (v[i] == '-') {
int hmm = v1[i];
if (v1[i] - 1 < barabo) {
barabo -= v1[i] - 1;
v1[i] = 1;
} else {
v1[i] -= barabo;
break;
}
}
}
for (int i = 1; i < sz; i++) cout << v1[i - 1] << " " << v[i] << " ";
cout << v1[sz - 1] << " = " << num;
} else
cout << "Impossible";
}
|
#include <bits/stdc++.h>
using namespace std;
string x;
long long int suma, V[200], neg, poz, suma1, nr1, nr2, k, i;
int main() {
cin >> x;
poz = 1;
while (cin >> x) {
if (x == "+") {
V[neg + poz] = 1;
poz++;
cin >> x;
} else if (x == "-") {
V[neg + poz] = 0;
neg++;
cin >> x;
} else if (x == "=") {
cin >> suma;
}
}
if (suma + neg > suma * poz || poz > suma + neg * suma)
cout << "Impossible";
else {
cout << "Possible \n";
k = 1;
int var = neg + poz;
while (suma + k * neg < poz) k++;
suma1 = suma + k * neg;
nr1 = suma1 / poz;
nr2 = suma1 - suma1 / poz * poz;
cout << nr1;
for (i = 1; i < var; i++) {
if (V[i] == 0)
cout << " - " << k;
else if (nr2 > 0) {
cout << " + " << nr1 + 1;
nr2--;
} else
cout << " + " << nr1;
}
cout << " = " << suma;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, jia, jian;
char ch[2000010];
int a[1000010];
bool b[1000010];
int main() {
gets(ch);
int i = 0;
jia = 0, jian = 0;
int s = 0;
while (ch[i] != '=') {
i++;
if (ch[i] == '-')
jian++, b[++s] = true;
else if (ch[i] == '+')
jia++, b[++s] = false;
}
int n = strlen(ch) - 1;
m = 0;
while (i < n) {
i++;
if (int(ch[i]) >= 48 && int(ch[i]) <= 48 + 9) m = m * 10 + int(ch[i]) - 48;
}
jia++;
int p = m;
b[0] = false;
if (m <= jia * m - jian && m >= jia - jian * m) {
cout << "Possible" << endl;
if (jia > jian) {
for (int i = 1; i <= s; i++)
if (b[i]) a[i] = p, m += p;
for (int i = 0; i <= s; i++)
if (!b[i]) a[i] = 1, m -= 1;
for (int i = 0; i <= s; i++)
if (!b[i]) {
if (m > p - 1)
m -= p - 1, a[i] = p;
else
a[i] = 1 + m, m = 0;
if (m == 0) break;
}
} else if (jia == jian) {
for (int i = 0; i <= s; i++) a[i] = 1;
for (int i = 0; i <= s; i++)
if (!b[i]) {
if (m > p - 1)
m -= p - 1, a[i] = p;
else
a[i] = 1 + m, m = 0;
if (m == 0) break;
}
} else {
for (int i = 0; i <= s; i++)
if (!b[i]) a[i] = p, m -= p;
for (int i = 1; i <= s; i++)
if (b[i]) a[i] = 1, m += 1;
for (int i = 1; i <= s; i++)
if (b[i]) {
if (m + p - 1 < 0)
m += p - 1, a[i] = p;
else
a[i] = -m + 1, m = 0;
if (m == 0) break;
}
}
cout << a[0];
for (int i = 1; i <= s; i++) {
if (b[i])
printf(" - ");
else
printf(" + ");
printf("%d", a[i]);
}
cout << " = " << p << endl;
} else
cout << "Impossible" << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
#pragma warning(disable : 4996)
#pragma hdrstop
template <bool... Bools>
struct Disjunction : std::false_type {};
template <bool First, bool... Others>
struct Disjunction<First, Others...>
: std::integral_constant<bool, First || Disjunction<Others...>::value> {};
template <typename T>
constexpr auto has_begin(int) -> decltype(std::begin(std::declval<T>()), true) {
return true;
}
template <typename T>
constexpr bool has_begin(...) {
return false;
}
template <typename T>
using is_container = std::integral_constant<bool, has_begin<T>(0)>;
template <typename T, typename... Types>
using is_one_of = Disjunction<std::is_same<T, Types>::value...>;
template <typename T>
using is_sane_integer = std::integral_constant<
bool,
std::is_integral<T>::value &&
!is_one_of<typename std::remove_cv<T>::type, char, signed char,
unsigned char, bool, char16_t, char32_t, wchar_t>::value>;
std::string to_string(const std::string& s) { return '"' + s + '"'; }
std::string to_string(const char* s) { return to_string(std::string(s)); }
std::string to_string(const char ch) { return {'\'', ch, '\''}; }
std::string to_string(const bool b) { return (b ? "true" : "false"); }
template <typename T,
typename std::enable_if<Disjunction<
is_sane_integer<T>::value,
std::is_floating_point<T>::value>::value>::type* = nullptr>
std::string to_string(const T value) {
return std::to_string(value);
}
template <typename A, typename B>
std::string to_string(const std::pair<A, B>&);
template <typename T,
typename std::enable_if<is_container<T>::value>::type* = nullptr>
std::string to_string(const T& container) {
bool is_first = true;
std::string res = "{";
for (const auto& x : container) {
if (!is_first) {
res += ", ";
}
is_first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
std::string to_string(const std::pair<A, B>& p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
std::vector<std::string> vec_splitter(std::string s) {
s += ',';
std::vector<std::string> res;
while (!s.empty()) {
const size_t pos = s.find(',');
res.emplace_back(s.substr(0, pos));
s = s.substr(pos + 1);
}
return res;
}
void debug_out(std::vector<std::string> __attribute__((unused)) args,
__attribute__((unused)) int idx,
__attribute__((unused)) int LINE_NUM) {
std::cerr << std::endl;
}
template <typename Head, typename... Tail>
void debug_out(std::vector<std::string> args, int idx, int LINE_NUM, Head H,
Tail... T) {
std::stringstream ss;
ss << to_string(H);
if (idx > 0) {
std::cerr << ", ";
} else {
std::cerr << "Line(" << LINE_NUM << ") ";
}
std::cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
namespace std {
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T>& vec) {
for (auto& it : vec) {
in >> it;
}
return in;
}
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U>& rhs) {
in >> rhs.first >> rhs.second;
return in;
}
} // namespace std
void assign_files_input_txt() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
template <typename T>
class IntegerIterator
: public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
using value_type = T;
constexpr explicit IntegerIterator(const value_type value) : value_(value) {}
IntegerIterator& operator++() {
++value_;
return *this;
}
IntegerIterator operator++(int) {
IntegerIterator copy = *this;
++value_;
return copy;
}
IntegerIterator& operator--() {
--value_;
return *this;
}
IntegerIterator operator--(int) {
IntegerIterator copy = *this;
--value_;
return copy;
}
constexpr value_type operator*() const { return value_; }
constexpr bool operator==(const IntegerIterator rhs) const {
return value_ == rhs.value_;
}
constexpr bool operator!=(const IntegerIterator rhs) const {
return !(*this == rhs);
}
constexpr value_type operator+(const IntegerIterator rhs) const {
return value_ + rhs.value_;
}
IntegerIterator& operator+=(const IntegerIterator rhs) const {
value_ += rhs.value_;
return *this;
}
constexpr value_type operator-(const IntegerIterator rhs) const {
return value_ - rhs.value_;
}
IntegerIterator& operator-=(const IntegerIterator rhs) const {
value_ -= rhs.value_;
return *this;
}
private:
value_type value_;
};
template <typename T>
class IntegerRange {
public:
using const_iterator = IntegerIterator<T>;
constexpr IntegerRange(const T begin, const T end)
: begin_(begin), end_(end) {}
constexpr const_iterator begin() const { return const_iterator(begin_); }
constexpr const_iterator end() const { return const_iterator(end_); }
constexpr T size() const { return end_ - begin_; }
private:
T begin_;
T end_;
};
template <typename T>
class ReversedRange {
public:
using const_iterator = typename std::decay<T>::type::const_iterator;
using reverse_iterator = std::reverse_iterator<const_iterator>;
explicit constexpr ReversedRange(const T& iterable)
: begin_(iterable.begin()), end_(iterable.end()) {}
constexpr reverse_iterator begin() const { return reverse_iterator(end_); }
constexpr reverse_iterator end() const { return reverse_iterator(begin_); }
private:
const_iterator begin_;
const_iterator end_;
};
template <typename T>
ReversedRange<T> reversed(T&& iterable) {
return ReversedRange<T>{iterable};
}
template <typename T>
constexpr IntegerRange<T> range(const T to) {
return IntegerRange<T>(0, to);
}
template <typename T>
constexpr IntegerRange<T> range(const T from, const T to) {
return IntegerRange<T>(from, to);
}
template <typename T>
constexpr IntegerRange<T> inclusiveRange(const T to) {
return IntegerRange<T>(0, to + 1);
}
template <typename T>
constexpr IntegerRange<T> inclusiveRange(const T from, const T to) {
return IntegerRange<T>(from, to + 1);
}
template <typename T>
constexpr ReversedRange<IntegerRange<T>> downrange(const T from) {
return reversed(range(from));
}
template <typename T>
constexpr ReversedRange<IntegerRange<T>> downrange(const T from, const T to) {
return reversed(range(to, from));
}
template <typename T>
constexpr ReversedRange<IntegerRange<T>> inclusiveDownrange(const T from) {
return reversed(range(from + 1));
}
template <typename T>
constexpr ReversedRange<IntegerRange<T>> inclusiveDownrange(const T from,
const T to) {
return reversed(range(to, from + 1));
}
template <typename R>
void sort(R& range) {
std::sort(range.begin(), range.end());
}
template <typename R, typename Comp>
void sort(R& range, Comp comp) {
std::sort(range.begin(), range.end(), comp);
}
template <typename R>
void reverse(R& range) {
std::reverse(range.begin(), range.end());
}
template <typename R, typename T>
auto lower_bound(const R& range, const T& value) -> decltype(range.begin()) {
return std::lower_bound(range.begin(), range.end(), value);
}
template <typename R, typename T, typename Comp>
auto lower_bound(const R& range, const T& value, Comp comp)
-> decltype(range.begin()) {
return std::lower_bound(range.begin(), range.end(), value, comp);
}
template <typename R, typename T>
auto upper_bound(const R& range, const T& value) -> decltype(range.begin()) {
return std::upper_bound(range.begin(), range.end(), value);
}
template <typename R, typename T, typename Comp>
auto upper_bound(const R& range, const T& value, Comp comp)
-> decltype(range.begin()) {
return std::upper_bound(range.begin(), range.end(), value, comp);
}
template <typename R>
auto min_element(const R& range) -> decltype(range.begin()) {
return std::min_element(range.begin(), range.end());
}
template <typename R>
auto max_element(const R& range) -> decltype(range.begin()) {
return std::max_element(range.begin(), range.end());
}
template <typename R>
bool next_permutation(R& range) {
return std::next_permutation(range.begin(), range.end());
}
template <typename T>
void unique(std::vector<T>& range) {
range.erase(std::unique(range.begin(), range.end()), range.end());
}
template <typename T>
void dedup(std::vector<T>& range) {
sort(range);
unique(range);
}
template <typename R>
R sorted(R range) {
sort(range);
return range;
}
template <typename R, typename Comp>
R sorted(R range, Comp comp) {
sort(range, comp);
return range;
}
static constexpr long double PI = 3.14159265358979323846;
static constexpr long double EPS = 1e-9;
static constexpr int32_t INF = 0x3f3f3f3f;
static constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
static constexpr int32_t DX[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
static constexpr int32_t DY[8] = {0, 1, 0, -1, -1, 1, 1, -1};
static constexpr uint32_t kDayMonth[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
enum Direction : uint8_t {
Up,
Right,
Down,
Left,
UpLeft,
UpRight,
DownRight,
DownLeft,
North = Up,
East = Right,
South = Down,
West = Left,
NorthWest = UpLeft,
NorthEast = UpRight,
SouthEast = DownRight,
SouthWest = DownLeft,
Unknown = 255
};
namespace Random {
inline std::chrono::system_clock::rep current_timestamp() {
return std::chrono::system_clock::now().time_since_epoch().count();
}
inline uint64_t get_new_rand_seed() {
return static_cast<uint64_t>(current_timestamp());
}
inline uint64_t get_rand_seed() {
static const uint64_t kRandSeed = get_new_rand_seed();
return kRandSeed;
}
using Engine = std::mt19937_64;
template <typename T>
using Distribution = std::uniform_int_distribution<T>;
inline Engine& random_engine() {
static Engine engine(get_rand_seed());
return engine;
}
template <typename T>
inline T get(const T to) {
Distribution<T> distribution(0, to);
return distribution(random_engine());
}
template <typename T>
inline T get(const T from, const T to) {
return get(to - from) + from;
}
template <typename T = int32_t>
inline T get() {
return get(static_cast<T>(0), std::numeric_limits<T>::max());
}
} // namespace Random
template <typename T,
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
class SafeIntegralHash {
public:
const T kRandomValue = static_cast<T>(Random::current_timestamp());
T operator()(const T& value) const { return value ^ kRandomValue; }
};
template <typename Key, typename Value,
typename std::enable_if<std::is_integral<Key>::value>::type* =
nullptr>
using SafeUnorderedMap = std::unordered_map<Key, Value, SafeIntegralHash<Key>>;
template <typename Key, typename Value,
typename std::enable_if<std::is_integral<Key>::value>::type* =
nullptr>
using SafeUnorderedMultiMap =
std::unordered_multimap<Key, Value, SafeIntegralHash<Key>>;
template <typename Key,
typename std::enable_if<std::is_integral<Key>::value>::type* =
nullptr>
using SafeUnorderedSet = std::unordered_set<Key, SafeIntegralHash<Key>>;
template <typename Key,
typename std::enable_if<std::is_integral<Key>::value>::type* =
nullptr>
using SafeUnorderedMultiSet =
std::unordered_multiset<Key, SafeIntegralHash<Key>>;
namespace std {
template <typename T, typename U>
struct hash<std::pair<T, U>> {
std::size_t operator()(const std::pair<T, U>& pair) const {
return pair.first * 877117 + pair.second;
}
};
} // namespace std
template <typename T, typename std::enable_if<
std::is_floating_point<T>::value>::type* = nullptr>
inline bool is_equal_to_zero(const T& a) {
return abs(a) < EPS;
}
template <typename T, typename std::enable_if<
!std::is_floating_point<T>::value>::type* = nullptr>
inline bool is_equal_to_zero(const T& a) {
return a == 0;
}
template <typename T>
inline bool umin(T& a, const T& b) {
return (b < a ? a = b, true : false);
}
template <typename T>
inline bool umax(T& a, const T& b) {
return (a < b ? a = b, true : false);
}
template <typename T, size_t N>
struct MakeVector {
template <typename... Args,
typename R = std::vector<decltype(MakeVector<T, N - 1>::make_vector(
std::declval<Args>()...))>>
static R make_vector(size_t first, Args... sizes) {
auto inner = MakeVector<T, N - 1>::make_vector(sizes...);
return R(first, inner);
}
};
template <typename T>
struct MakeVector<T, 1> {
template <typename R = std::vector<T>>
static R make_vector(std::size_t size, const T& value) {
return R(size, value);
}
};
template <typename T, typename... Args>
auto make_vector(Args... args)
-> decltype(MakeVector<T, sizeof...(Args) - 1>::make_vector(args...)) {
return MakeVector<T, sizeof...(Args) - 1>::make_vector(args...);
}
template <typename T>
inline bool is_leap_year(const T year) {
return year % 400 == 0 || (year % 100 != 0 && (year & 3) == 0);
}
inline size_t get_days_in_month(const size_t month, const size_t year) {
return kDayMonth[month] + (month == 1 && is_leap_year(year) ? 1 : 0);
}
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using pli = std::pair<ll, int>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vp = std::vector<pii>;
class ARebus {
public:
static constexpr bool kWriteCaseNumber = false;
static constexpr bool kMultiTest = false;
void solve(std::istream& in, std::ostream& out) {
std::string s;
std::getline(in, s);
std::vector<std::string> tokens = {""};
tokens.reserve(s.size());
for (char ch : s) {
if (isspace(ch)) {
if (tokens.empty() || !tokens.back().empty()) {
tokens.emplace_back("");
}
} else {
tokens.back() += ch;
}
}
while (!tokens.empty() && tokens.back().empty()) {
tokens.pop_back();
}
42;
const int N = std::stoi(tokens.back());
bool plus = true;
std::vector<int> ans;
ans.reserve(tokens.size() / 2);
int sum = 0;
for (const std::string& token : tokens) {
if (token == "+") {
plus = true;
} else if (token == "-") {
plus = false;
} else if (token == "=") {
break;
} else if (token == "?") {
ans.emplace_back(plus ? 1 : -1);
sum += ans.back();
} else {
assert(false);
}
}
for (int& x : ans) {
if (sum < N) {
if (x > 0) {
sum -= x;
x = std::min(N, N - sum);
sum += x;
}
}
if (sum > N) {
if (x < 0) {
sum -= x;
x = -std::min(N, sum - N);
sum += x;
}
}
}
if (sum != N) {
out << "Impossible" << std::endl;
return;
}
out << "Possible" << std::endl;
size_t index = 0;
for (const auto& token : tokens) {
if (token == "?") {
out << abs(ans[index++]) << " ";
} else {
out << token << " ";
}
}
out << std::endl;
}
ARebus() {}
static constexpr bool kUseCustomChecker = false;
bool check(std::istringstream in, std::istringstream jury,
std::istringstream out) {
return true;
}
static constexpr size_t kGeneratedTestsCount = 0;
static void generate_test(std::ostream& test) {}
};
int main() {
ARebus solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
srand(time(nullptr));
out << std::fixed << std::setprecision(16);
if (ARebus::kMultiTest) {
size_t tests_count;
in >> tests_count;
for (size_t test_id : inclusiveRange<size_t>(1, tests_count)) {
if (ARebus::kWriteCaseNumber) {
out << "Case #" << test_id << ": ";
}
solver.solve(in, out);
}
} else {
solver.solve(in, out);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int c[2] = {1, 0}, n;
string sign = "+";
vector<int> ans;
bool possible() {
int d = n, lo, hi;
for (char s : sign) {
if (s == '+') {
c[0]--;
hi = min(n, c[1] * n - c[0] + d);
lo = max(1, c[1] - c[0] * n + d);
d -= lo;
} else {
c[1]--;
hi = min(n, c[0] * n - c[1] - d);
lo = max(1, c[0] - c[1] * n - d);
d += lo;
};
if (lo > hi) return false;
ans.push_back(lo);
}
return true;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed;
for (string tok;;) {
cin >> tok >> tok;
if (tok[0] == '=') break;
sign += tok[0];
c[tok[0] == '+' ? 0 : 1]++;
}
cin >> n;
;
;
if (possible()) {
cout << "Possible" << endl;
for (size_t i = 0; i < sign.size(); i++) {
if (i > 0) cout << sign[i] << ' ';
cout << ans[i] << ' ';
}
cout << "= " << n << endl;
} else {
cout << "Impossible" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
T sqr(const T &x) {
return x * x;
}
template <class T>
T lowbit(const T &x) {
return (x ^ (x - 1)) & x;
}
template <class T>
int countbit(const T &n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
void ckmin(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
void ckmax(T &a, const T &b) {
if (b > a) a = b;
}
int n;
int c1, c2;
int m, a[256];
int r1[256], r2[256];
void solve(int r[], int c, int s) {
s -= c;
for (int i = 0; i < (c); ++i) {
r[i] = min(n - 1, s);
s -= r[i];
r[i]++;
}
}
int main() {
std::ios_base::sync_with_stdio(false);
m = c1 = c2 = 0;
string s;
while (cin >> s)
if (s == "?" || s == "+" || s == "-") {
a[m++] = (s == "-" ? -1 : 1);
if (s != "?") cin >> s;
} else {
cin >> n;
break;
}
for (int i = 0; i < (m); ++i)
if (a[i] > 0)
c1++;
else
c2++;
bool ok = false;
vector<int> p;
p.push_back(c1);
p.push_back(c2 + n);
p.push_back(c1 * n);
p.push_back(c2 * n + n);
for (int s1 : p) {
int s2 = s1 - n;
if (s1 >= c1 && s1 <= c1 * n && s2 >= c2 && s2 <= c2 * n) {
ok = true;
solve(r1, c1, s1);
solve(r2, c2, s2);
break;
}
}
if (!ok) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
for (int i = 0; i < (m); ++i)
if (a[i] > 0) {
if (i > 0) printf(" + ");
printf("%d", r1[--c1]);
} else {
if (i > 0) printf(" - ");
printf("%d", r2[--c2]);
}
printf(" = %d\n", n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200;
int N, L;
int a[MAXN], sgn[MAXN];
int main() {
string s, t;
cin >> s;
sgn[L++] = 1;
while (cin >> s) {
cin >> t;
if (s == "+")
sgn[L++] = 1;
else if (s == "-")
sgn[L++] = -1;
else {
N = atoi(t.c_str());
}
}
int sum = 0;
for (int i = 0; i < L; i++) {
sum += sgn[i];
a[i] = 1;
}
for (int i = 0; i < L; i++) {
if (sum < N && sgn[i] == 1) {
while (a[i] < N && sum < N) {
a[i]++;
sum++;
}
} else if (sum > N && sgn[i] == -1) {
while (a[i] < N && sum > N) {
a[i]++;
sum--;
}
}
}
if (sum != N)
puts("Impossible");
else {
puts("Possible");
printf("%d ", a[0]);
for (int i = 1; i < L; i++) printf("%c %d ", sgn[i] == 1 ? '+' : '-', a[i]);
printf("= %d\n", N);
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
int n, m, i, j, k, l, p = 1, q = 0, x;
v.push_back(1);
while (1) {
cin >> s;
if (s[0] == '=') break;
if (s[0] == '?') continue;
if (s[0] == '+') {
v.push_back(1);
p++;
}
if (s[i] == '-') {
v.push_back(2);
q++;
}
}
cin >> n;
m = n - p + q;
if (m >= 0) {
if (m > (n - 1) * p) {
cout << "Impossible";
return 0;
} else {
cout << "Possible\n";
for (k = 0; k < v.size(); k++) {
if (k != 0) {
if (v[k] == 1)
cout << "+ ";
else
cout << "- ";
}
if (v[k] == 1) {
if (m >= (n - 1)) {
m -= (n - 1);
x = (n - 1);
} else {
x = m;
m = 0;
}
cout << x + 1 << " ";
} else {
cout << "1 ";
}
}
}
} else {
m = -m;
if (m > (n - 1) * q) {
cout << "Impossible";
return 0;
} else {
cout << "Possible\n";
for (k = 0; k < v.size(); k++) {
if (k != 0) {
if (v[k] == 1)
cout << "+ ";
else
cout << "- ";
}
if (v[k] == 2) {
if (m >= (n - 1)) {
m -= (n - 1);
x = (n - 1);
} else {
x = m;
m = 0;
}
cout << x + 1 << " ";
} else {
cout << "1 ";
}
}
}
}
cout << "= " << n;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long ans = 1;
while (b > 0) {
if (b % 2 != 0) {
ans = (ans * a) % 1000000007;
}
a = (a * a) % 1000000007;
b >>= 1;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t = 1;
while (t--) {
vector<string> s;
string h;
while (cin >> h) {
s.push_back(h);
}
int z = stoi(s[s.size() - 1]);
if (s.size() == 3) {
cout << "Possible" << endl << z << " = " << z;
return 0;
}
int n = 0, m = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == "+") n++;
if (s[i] == "-") m++;
}
n++;
int p, q;
int o = -1;
for (q = 1; q <= z; q++) {
p = ceil((z + m * q) / double(n));
int hap = (z + m * q) / n;
if (hap == 0) continue;
if (p >= 1 && p <= z) {
o = q;
break;
}
}
if (o == -1) {
cout << "Impossible";
return 0;
} else
cout << "Possible" << endl;
q = o;
o = z + m * q;
p = ceil(o / double(n));
int cnt = o % (n);
vector<string> v;
v.push_back(to_string(p));
cnt--;
for (int i = 1; i < s.size(); i++) {
if (s[i] != "?") {
v.push_back(s[i]);
continue;
}
if (v[v.size() - 1] == "-") {
v.push_back(to_string(q));
continue;
}
if (cnt == 0) {
v.push_back(to_string(p - 1));
} else {
v.push_back(to_string(p));
cnt--;
}
}
for (auto i : v) cout << i << " ";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<char> sign;
int main() {
int pos = 1;
int neg = 0;
sign.push_back('+');
scanf("%*c ");
while (true) {
char c;
scanf("%c ", &c);
if (c == '=') break;
if (c == '+') pos += 1;
if (c == '-') neg += 1;
sign.push_back(c);
scanf("%*c ");
}
scanf("%d", &n);
if (n < (pos * 1 - neg * n) || (pos * n - neg) < n) {
printf("Impossible");
return 0;
}
printf("Possible\n");
int ext = (pos * n - neg) - n;
bool first = true;
for (char c : sign) {
if (first) {
first = false;
} else {
printf("%c ", c);
}
if (c == '+')
printf("%d ", max(1, n - ext));
else
printf("%d ", min(n, 1 + ext));
ext = max(0, ext - n + 1);
}
printf("= %d", n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int arr[200];
int coef[200];
int main() {
int p = 1, m = 0;
char ch;
coef[1] = 1;
int tt = 1;
int len = 0;
while (1) {
char qq[5], ch;
scanf("%s", qq);
ch = qq[0];
if (ch == '?') ++len;
if (ch == '+') coef[len + 1] = 1, ++p;
if (ch == '-') coef[len + 1] = -1, ++m;
if (ch == '=') break;
}
int tar;
cin >> tar;
int sum = (p - m) * tar;
for (int j = 1; j <= len; j++) arr[j] = tar;
for (int j = 1; j <= len && sum != tar; j++) {
arr[j] = tar;
if (coef[j] == 1) {
while (arr[j] > 1 && sum > tar) arr[j]--, sum--;
} else {
while (arr[j] > 1 && sum < tar) arr[j]--, sum++;
}
}
if (sum != tar) {
puts("Impossible");
return 0;
}
puts("Possible");
printf("%d ", arr[1]);
for (int j = 2; j <= len; j++) {
if (coef[j] == 1)
printf("+ ");
else
printf("- ");
printf("%d ", arr[j]);
}
printf("= %d", tar);
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> giho, p, m;
int main() {
int n, i, j;
string s;
getline(cin, s);
int prev = 1, pos = 0;
for (i = 0; i < s.size(); ++i) {
if (s[i] == '=') break;
if (s[i] == ' ') continue;
if (s[i] == '?') {
if (prev) {
p.push_back(pos++);
} else
m.push_back(pos++);
}
if (s[i] == '+' || s[i] == '-') {
if (s[i] == '+')
prev = 1, giho.push_back(1);
else
prev = 0, giho.push_back(0);
}
}
i += 2;
n = 0;
for (; i < s.size(); ++i) n = n * 10 + s[i] - '0';
int x, y, u = p.size(), v = m.size();
x = u * n - v;
y = u - v * n;
if (y <= n && n <= x) {
cout << "Possible\n";
int ret[150];
int k = 1e9, z;
for (i = v; i <= n * v; ++i) {
if (n + i >= u) {
k = n + i, z = i;
break;
}
}
assert(k <= n * u);
int rem, g;
for (i = 0; i < u; ++i) {
rem = u - i - 1;
g = min(k - rem, n);
k -= g;
ret[p[i]] = g;
}
for (i = 0; i < v; ++i) {
rem = v - i - 1;
g = min(z - rem, n);
z -= g;
ret[m[i]] = g;
}
pos = 0;
for (i = 0; i < u + v; ++i) {
cout << ret[i];
if (pos < giho.size()) {
if (giho[pos++])
cout << " + ";
else
cout << " - ";
}
}
cout << " = " << n << endl;
} else {
cout << "Impossible\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
void kill() {
cout << "Impossible";
exit(0);
}
int po = 1, ne = 0;
void print(int a, int b) {
cout << "Possible" << endl;
char last = '+';
int target1 = 0, cnt1 = 0;
int target2 = 0, cnt2 = 0;
if (po) {
target1 = a / po;
cnt1 = a % po;
}
if (ne) {
target2 = b / ne;
cnt2 = b % ne;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '=') {
cout << s.substr(i);
return;
}
if (s[i] == ' ') {
cout << ' ';
continue;
}
if (s[i] == '?') {
if (last == '+') {
if (cnt1) {
cout << target1 + 1;
cnt1--;
} else {
cout << target1;
}
} else {
if (cnt2) {
cout << target2 + 1;
cnt2--;
} else {
cout << target2;
}
}
} else {
cout << s[i];
last = s[i];
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
getline(cin, s);
int n = 0;
int j = s.size() - 1, d = 1;
while (s[j] != ' ') {
n += d * (s[j] - '0');
j--;
d *= 10;
}
for (int i = 0; i < s.size() && s[i] != '='; i++) {
if (s[i] == '?') continue;
if (s[i] == ' ') continue;
if (s[i] == '+')
po++;
else
ne++;
}
if (po - ne == n) print(po, ne);
if (po - ne > n) {
int x = po - n;
if (x > n * ne) kill();
print(po, x);
}
if (po - ne < n) {
int x = n + ne;
if (x > n * po) kill();
print(x, ne);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e9, mxs = 1e6 + 100;
string s, Str;
long long Ans, first = 1, c;
long long x1 = 1, x2;
void Sum1() {
cout << Ans << ' ', --x1;
long long Save = (x1 * Ans), cp;
cp = Save % x2;
Save /= x2;
for (long long i = 1; i < Str.size() - c - 1; i += 2) {
if (Str[i] == '+')
cout << "+ " << Ans << ' ';
else {
if (cp)
cout << "- " << Save + 1 << ' ', --cp, --x2;
else if (x2)
cout << "- " << Save << ' ', --x2;
}
}
cout << "= " << Ans;
}
void Sum2() {
cout << (Ans + Ans * x2) / x1 << ' ';
long long o = (Ans + Ans * x2), z = o / x1;
--x1;
if (x1 == 0)
cout << "= " << Ans;
else {
long long Save = (o - z), cp;
cp = Save % x1, Save /= x1;
for (long long i = 1; i < Str.size() - c - 1; i += 2) {
if (Str[i] == '-')
cout << "- " << Ans << ' ';
else {
if (cp)
cout << "+ " << Save + 1 << ' ', --cp, --x1;
else if (x1)
cout << "+ " << Save << ' ', --x1;
}
}
cout << "= " << Ans;
}
}
int main() {
getline(cin, s);
for (long long t = 0; t < s.size(); ++t) {
if (s[t] == '+')
x1++;
else if (s[t] == '-')
x2++;
if (s[t] != ' ') Str += s[t];
}
for (long long t = s.size() - 1; t >= 0; --t) {
if ('0' <= s[t] && s[t] <= '9') {
Ans += (s[t] - '0') * first;
first *= 10;
c++;
}
}
if (Ans * x1 - x2 < Ans || x1 - Ans * x2 > Ans)
return cout << "Impossible", 0;
else {
cout << "Possible\n", (x1 <= x2 ? Sum1() : Sum2());
}
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int x, a = 1, b, tmp;
vector<int> manf;
bool flg;
int main() {
getline(cin, s);
for (int i = 2; i < s.length(); i += 2) {
if (s[i] == '+')
a++;
else if (s[i] == '-')
b++;
else if (s[i] == '=') {
for (int j = i + 2; j < s.length(); j++) x = x * 10 + (s[j] - '0');
break;
}
}
if (a * x < x + b || a > x + b * x) {
cout << "Impossible";
return 0;
}
if (a - b > x) {
tmp = a - x;
for (int i = 0; i < b; i++) {
manf.push_back(min(x, tmp - b + i + 1));
tmp -= min(x, tmp - b + i + 1);
}
tmp = a - x;
} else {
tmp = b;
for (int i = 0; i < b; i++) manf.push_back(1);
}
tmp += x;
cout << "Possible" << endl;
for (int i = 0; i < s.length(); i += 2) {
if (s[i] == '?') {
if (flg) {
cout << manf.back() << " ";
manf.pop_back();
} else {
a--;
cout << min(x, tmp - a) << " ";
tmp -= min(x, tmp - a);
}
} else if (s[i] == '+') {
flg = 0;
cout << s[i] << " ";
} else if (s[i] == '-') {
flg = 1;
cout << s[i] << " ";
} else if (s[i] == '=') {
cout << "= " << x;
break;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
char s;
int st = 1, n;
vector<int> cf;
while (true) {
cin >> s;
if (s == '=') break;
if (s == '?')
cf.push_back(st);
else if (s == '+')
st = 1;
else
st = -1;
}
cin >> n;
int l = 0, r = 0;
vector<int> ans;
for (int it : cf) {
if (it == 1) {
l++;
r += n;
} else {
--r;
l -= n;
}
ans.push_back(it == 1 ? n : 1);
}
if (l > n || r < n) {
cout << "Impossible";
return 0;
}
for (int i = 0; i < int(ans.size()); i++) {
int old = ans[i];
if (cf[i] == 1)
ans[i] = max(1, ans[i] - (r - n));
else
ans[i] = min(n, ans[i] + (r - n));
r -= (old - ans[i]) * cf[i];
}
cout << "Possible\n";
for (int i = 0; i < int(ans.size()); i++) {
cout << ans[i] << " ";
if (i < int(ans.size()) - 1) cout << (cf[i + 1] == 1 ? "+ " : "- ");
}
cout << "= " << n;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char c1[10000000];
string s;
int n, numM, numS;
stack<string> conA, conB;
string convert_string(int m) {
string res = "";
while (m != 0) {
res = (char)(m % 10 + '0') + res;
m /= 10;
}
return res;
}
int main() {
getline(cin, s);
int t = s.length() - 1;
while (s[t - 1] != ' ') t--;
for (int i = t; i < s.length(); i++) n = n * 10 + (s[i] - '0');
numS = 1;
numM = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '+') numS++;
if (s[i] == '-') numM++;
}
bool res = true;
int num1, num0 = numM, carryN = n;
while ((num0 > 0) && ((n + num0) < numS)) {
num0--;
n += carryN;
conB.push(convert_string(carryN));
}
n += num0;
while (num0--) {
conB.push(convert_string(1));
}
t = n / numS;
if (t == 0) {
res = false;
} else {
if (t > carryN) {
res = false;
} else {
if (n % numS != 0) {
if (t + 1 > carryN) {
res = false;
} else {
num1 = n % numS;
}
}
}
}
if (res) {
cout << "Possible\n";
string ss = "";
numS -= num1;
while (num1--) {
conA.push(convert_string(t + 1));
}
while (numS--) conA.push(convert_string(t));
ss += conA.top();
conA.pop();
for (int i = 1; i < s.length(); i++) {
if (s[i] == '?') {
if (s[i - 2] == '+') {
ss += conA.top();
conA.pop();
} else {
ss += conB.top();
conB.pop();
}
} else {
ss += s[i];
}
}
cout << ss << endl;
} else {
cout << "Impossible" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, queCnt, addCnt, subCnt;
vector<int> sign, res[2];
string s;
int main() {
getline(cin, s);
n = 0;
addCnt = 1;
subCnt = 0;
sign.push_back(1);
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '?') queCnt++;
if (s[i] == '-') subCnt++, sign.push_back(0);
if (s[i] == '+') addCnt++, sign.push_back(1);
if (s[i] >= '0' && s[i] <= '9') n = 10 * n + (s[i] - '0');
}
for (int i = subCnt; i <= n * subCnt; ++i) {
int rem = n + i;
if (rem >= addCnt && rem <= n * addCnt) {
int x = rem, y = i;
cout << "Possible" << endl;
for (int j = 0; j < addCnt; ++j)
if (j == addCnt - 1)
res[0].push_back(x);
else
res[0].push_back(1), x--;
if (addCnt >= 2) {
while (res[0][addCnt - 1] > n) {
int z = addCnt - 1;
while (res[0][addCnt - 1] - z <= 0) z--;
res[0][addCnt - 1] -= z;
for (int j = 0; j <= z - 1; ++j) res[0][j]++;
}
}
for (int j = 0; j < subCnt; ++j)
if (j == subCnt - 1)
res[1].push_back(y);
else
res[1].push_back(1), y--;
if (subCnt >= 2) {
while (res[1][subCnt - 1] > n) {
int z = subCnt - 1;
while (res[1][subCnt - 1] - z <= 0) z--;
res[1][subCnt - 1] -= z;
for (int j = 0; j <= z - 1; ++j) res[1][j]++;
}
}
x = 0, y = 0;
for (int j = 0; j < sign.size(); ++j) {
if (j != 0) cout << (sign[j] == 0 ? " - " : " + ");
if (sign[j] == 0)
cout << res[1][x], x++;
else
cout << res[0][y], y++;
}
cout << " = " << n << endl;
exit(0);
}
}
cout << "Impossible" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> V;
vector<char> V2;
vector<char> V3;
int main() {
char z;
int mi = 0, pi = 1;
while (cin >> z) {
if (z == '?')
V3.push_back('?');
else if (z == '+') {
pi++;
V2.push_back(z);
} else if (z == '-') {
mi++;
V2.push_back(z);
} else if (z == '=')
break;
}
int n;
cin >> n;
int Min = pi - (n * mi);
int Max = n * pi - (mi);
if (n > Max || n < Min) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
vector<int> neg;
vector<int> pos;
if (mi > pi) {
int res = 0;
while (mi--) {
neg.push_back(1);
res--;
}
int count = n - res;
while (pi--) {
pos.push_back(1);
count--;
}
for (int i = 0; i < pos.size(); i++) {
int x = min(n - 1, count);
pos[i] += x;
count -= x;
}
} else if (pi > mi) {
int res = 0;
pos.push_back(n);
for (int i = 1; i < pi; i++) {
pos.push_back(1);
res++;
}
while (mi--) {
neg.push_back(1);
res--;
}
int count1 = pi - 1, count2 = 0;
for (int i = 0; i < neg.size(); i++) {
int x = min(res, n - 1);
res -= x;
neg[i] += x;
count2 += neg[i];
}
int x = count1 - count2;
pos[0] -= x;
} else {
pos.push_back(n);
pos.push_back(2);
for (int i = 2; i < pi; i++) {
pos.push_back(1);
}
for (int i = 0; i < mi; i++) {
neg.push_back(1);
}
}
cout << pos[0];
int c1 = 1, c2 = 0;
for (int i = 0; i < V2.size(); i++) {
cout << ' ' << V2[i];
if (V2[i] == '+') {
cout << ' ' << pos[c1];
c1++;
} else {
cout << ' ' << neg[c2];
c2++;
}
}
cout << " = " << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smax(T &x, T y) {
x = max((x), (y));
}
template <class T>
inline void smin(T &x, T y) {
x = min((x), (y));
}
inline void sc(int &x) {
bool f = 0;
x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return;
}
void sc(char &x) { scanf("%c", &x); }
void sc(int &x, int &y) {
sc(x);
return sc(y);
}
void sc(int &x, int &y, int &z) {
sc(x);
sc(y);
return sc(z);
}
const double eps = 1e-7;
int n, k;
char str[2000005];
int main() {
std::ios::sync_with_stdio(false);
char c;
vector<char> inp;
cin >> c;
cin >> c;
inp.push_back('+');
while (c != '=') {
inp.push_back(c);
cin >> c;
cin >> c;
}
cin >> n;
if (count((inp).begin(), (inp).end(), '+') >
(count((inp).begin(), (inp).end(), '-') + 1) * n ||
count((inp).begin(), (inp).end(), '-') >
(count((inp).begin(), (inp).end(), '+') - 1) * n) {
cout << "Impossible\n" << endl;
return 0;
}
int full = 0, rem = 0;
int sum = n - count((inp).begin(), (inp).end(), '+') +
count((inp).begin(), (inp).end(), '-');
cout << "Possible\n";
;
if (sum >= 0) {
full = sum / count((inp).begin(), (inp).end(), '+');
rem = sum % count((inp).begin(), (inp).end(), '+');
;
cout << (rem > 0 ? rem--, full + 2 : full + 1);
for (int i = 1; i < int((inp).size()); i++) {
if (inp[i] == '+')
cout << " + " << (rem > 0 ? rem--, full + 2 : full + 1);
else
cout << " - " << 1;
}
cout << " = " << n << endl;
} else {
full = sum / count((inp).begin(), (inp).end(), '-');
rem = abs(sum % count((inp).begin(), (inp).end(), '-'));
;
cout << 1;
for (int i = 1; i < int((inp).size()); i++) {
if (inp[i] == '-')
cout << " - " << (rem > 0 ? rem--, -(full - 2) : -(full - 1));
else
cout << " + " << 1;
}
cout << " = " << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
int plus, minus, n, extra = 0;
vector<int> seq(1, 1);
while (1) {
cin >> c;
if (c == '+')
seq.push_back(1);
else if (c == '-')
seq.push_back(-1);
else if (c == '=')
break;
}
cin >> n;
plus = count(seq.begin(), seq.end(), 1);
minus = count(seq.begin(), seq.end(), -1);
vector<int> ans(seq.size(), 1);
if (n < (plus - minus * n) || n > (plus * n - minus))
cout << "Impossible\n";
else {
cout << "Possible\n";
extra = plus - minus - n;
for (int i = 0; i < seq.size(); i++) {
if (extra == 0)
break;
else {
extra = extra - seq[i] * ans[i];
if (extra * seq[i] > 0)
ans[i] = max(1, min(n, -seq[i] * extra));
else
ans[i] = min(n, max(1, -seq[i] * extra));
extra = extra + seq[i] * ans[i];
}
}
cout << ans[0];
for (int i = 1; i < seq.size(); i++)
cout << (seq[i] == 1 ? " +" : " -") << " " << ans[i];
cout << " = " << n;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
int cnt = 1, arr[101], pos = 1, n, neg;
arr[0] = 1;
while (scanf("%c", &c) && c != '=') {
if (c != '+' && c != '-') continue;
arr[cnt] = (c == '+');
pos += arr[cnt++];
}
cin >> n;
neg = cnt - pos;
if (pos * n < neg + n || (pos > neg * n + n)) {
cout << "Impossible" << endl;
return 0;
}
cout << "Possible" << endl;
int sum = 0;
for (int i = 0; i < cnt; i++) {
if (arr[i]) {
if (i) cout << " + ";
cout << min(n, max(1, ((n + neg - sum) / pos)));
sum += min(n, max(1, ((n + neg - sum) / pos)));
pos--;
} else {
cout << " - ";
cout << min(n, max(1, ((pos * n - n) + sum) / neg));
sum -= min(n, max(1, ((pos * n - n + sum) / neg)));
neg--;
}
}
printf(" = %d\n", n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
const int MAXN = (int)1e6 + 10;
string s;
int n, sum, m;
int flag = 1;
bool mi;
vector<int> a;
int ans[MAXN];
int main() {
ios_base ::sync_with_stdio(false);
s = "";
while (s != "=") {
cin >> s;
if (s == "+") flag = 1;
if (s == "-") flag = -1;
if (s == "?") a.push_back(flag);
}
for (int i = 0; i < a.size(); i++) {
sum += a[i];
if (a[i] == -1) mi = true;
}
m = a.size();
cin >> n;
sum = 0;
for (int i = 0; i < m; i++) {
ans[i] = 1;
sum += ans[i] * a[i];
}
for (int i = 0; i < m; i++) {
if (a[i] == 1)
while (ans[i] < n && sum < n) ans[i]++, sum++;
if (a[i] == -1)
while (ans[i] < n && sum > n) ans[i]++, sum--;
}
if (sum != n) {
cout << "Impossible";
return 0;
}
cout << "Possible"
<< "\n";
cout << ans[0] << " ";
for (int i = 1; i < m; i++) {
if (a[i] == -1)
cout << "- ";
else
cout << "+ ";
cout << ans[i] << " ";
}
cout << "= " << n;
}
|
#include <bits/stdc++.h>
using namespace std;
void init() {}
const long double EPS = 1e-12;
int main() {
init();
vector<char> p;
long long n = 0, a = 1, b = 0;
string s;
while (true) {
cin >> s;
if (s == "+") {
p.push_back('+');
a++;
}
if (s == "-") {
p.push_back('-');
b++;
}
if ('0' <= s[0] && s[0] <= '9') {
for (int i = 0; i < (int)s.length(); i++) n = n * 10 + s[i] - '0';
break;
}
}
if (a * n - b < n || a - b * n > n) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
long long sa = n * a;
long long sb = b;
long long d = sa - sb - n;
vector<long long> aa;
for (int i = 0; i < a; i++) aa.push_back(n);
vector<long long> bb;
for (int i = 0; i < b; i++) bb.push_back(1);
for (int i = 0; d > 0 && i < a; i++) {
if (d > n - 1) {
aa[i] = 1;
d -= n - 1;
} else {
aa[i] -= d;
d = 0;
}
}
for (int i = 0; d > 0 && i < b; i++) {
if (d > n - 1) {
bb[i] = n;
d -= n - 1;
} else {
bb[i] += d;
d = 0;
}
}
cout << aa.back();
aa.pop_back();
for (int i = 0; i < (int)p.size(); i++) {
cout << " " << p[i] << " ";
if (p[i] == '+') {
cout << aa.back();
aa.pop_back();
} else {
cout << bb.back();
bb.pop_back();
}
}
cout << " = " << n << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char S[2];
int n, m, x, y, z, k, w;
int add, minu;
int A[105], ANS[105];
int main() {
add = 1;
A[0] = 1;
while (1) {
scanf("%s", S);
if (S[0] == '?') n++;
if (S[0] == '-')
minu++;
else if (S[0] == '+')
add++;
if (S[0] == '-')
A[++w] = 0;
else if (S[0] == '+')
A[++w] = 1;
if (S[0] == '=') break;
}
scanf("%d", &x);
if (add == 1 && minu >= 1) {
printf("Impossible\n");
return 0;
} else if (add == 1 && minu == 0) {
printf("Possible\n%d = %d\n", x, x);
return 0;
}
ANS[0] = x;
for (int i = (1); i <= (w); i++)
if (A[i] == 0)
ANS[i] = -1;
else
ANS[i] = 1;
y = 0;
for (int i = (0); i <= (w); i++) y += ANS[i];
if (y > x) {
for (int i = (0); i <= (w); i++) {
while (ANS[i] > 1 && y > x) {
ANS[i]--;
y--;
}
while (ANS[i] <= -1 && ANS[i] > -x && y > x) {
ANS[i]--;
y--;
}
}
} else {
for (int i = (0); i <= (w); i++) {
while (ANS[i] >= 1 && ANS[i] < x && y < x) {
ANS[i]++;
y++;
}
}
}
if (y != x) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
for (int i = (0); i <= (w); i++) {
printf("%d", abs(ANS[i]));
if (i == w) break;
if (A[i + 1] == 0)
printf(" - ");
else
printf(" + ");
}
printf(" = %d\n", x);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& _p) {
return os << "(" << _p.first << "," << _p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& _V) {
bool f = true;
os << "[";
for (auto v : _V) {
os << (f ? "" : ",") << v;
f = false;
}
return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& _S) {
bool f = true;
os << "(";
for (auto s : _S) {
os << (f ? "" : ",") << s;
f = false;
}
return os << ")";
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& _M) {
return os << set<pair<T, U>>(_M.begin(), _M.end());
}
const signed long long INF = 1000000100;
const long double EPS = 1e-9;
vector<bool> pieprzo;
int xs[123];
int LAST;
int N = 0;
string YES = "Possible";
string NO = "Impossible";
void read_data() {
string s;
bool positive = true;
while (true) {
cin >> s;
if (s == "?") {
pieprzo.push_back(positive);
++N;
}
if (s == "+") positive = true;
if (s == "-") positive = false;
if (s == "=") {
cin >> LAST;
break;
}
}
}
void solve() {
int MAX = LAST;
int min_sum = 0;
int max_sum = 0;
for (int(i) = (0); (i) < (N); (i)++) {
min_sum += pieprzo[i] ? 1 : -MAX;
max_sum += pieprzo[i] ? MAX : -1;
}
if (!(min_sum <= LAST and LAST <= max_sum)) {
cout << NO << endl;
return;
}
int sum = 0;
for (int(i) = (0); (i) < (N); (i)++) {
xs[i] = 1;
sum += pieprzo[i] ? 1 : -1;
}
for (int(i) = (0); (i) < (N); (i)++) {
while (sum < LAST and pieprzo[i] and xs[i] != MAX) {
++xs[i];
++sum;
}
while (sum > LAST and !pieprzo[i] and xs[i] != MAX) {
++xs[i];
--sum;
}
}
cout << YES << endl;
for (int(i) = (0); (i) < (N); (i)++) {
if (i == 0) cout << xs[i] << " ";
if (i != 0) cout << (pieprzo[i] ? " + " : " - ") << xs[i];
}
cout << " = " << LAST << endl;
}
int main() {
read_data();
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
char a;
int k = 0;
vector<int> s;
while (1) {
cin >> a >> a;
k++;
if (a == '+')
s.push_back(1);
else if (a == '-')
s.push_back(-1);
else
break;
}
int n;
cin >> n;
int plus = 0, minus = 0;
for (int i = 0; i < k - 1; i++) {
if (s[i] == 1)
plus++;
else
minus++;
}
if (minus > n * plus || plus + 1 > n * (minus + 1)) {
cout << "Impossible" << endl;
return 0;
}
if (plus + 1 > n + minus) {
cout << "Possible" << endl;
int x = n + n * minus - plus - 1;
if (x > n - 1) {
cout << n;
x -= (n - 1);
} else {
cout << x + 1;
x = 0;
}
for (int i = 0; i < k - 1; i++) {
if (s[i] == 1) {
cout << " + ";
if (x > n - 1) {
cout << n;
x -= (n - 1);
} else {
cout << x + 1;
x = 0;
}
} else {
cout << " - " << n;
}
}
cout << " = " << n << endl;
return 0;
}
cout << "Possible" << endl;
int x = n + minus - plus - 1;
if (x > n - 1) {
cout << n;
x -= (n - 1);
} else {
cout << x + 1;
x = 0;
}
for (int i = 0; i < k - 1; i++) {
if (s[i] == 1) {
cout << " + ";
if (x > n - 1) {
cout << n;
x -= (n - 1);
} else {
cout << x + 1;
x = 0;
}
} else {
cout << " - 1";
}
}
cout << " = " << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "";
char tmp;
int pos = 0, neg = 0, ques = 0;
while (scanf("%c", &tmp) != -1 && tmp != '=') {
if (tmp == '+')
pos++;
else if (tmp == '-')
neg++;
else if (tmp == '?')
ques++;
s += tmp;
}
int n;
scanf("%d", &n);
pos = ques - neg;
int plusVal = n + neg;
int each = 0, leftOver = 0;
if (pos > 0) {
each = plusVal / pos;
leftOver = plusVal % pos;
}
vector<int> v;
int possible = true;
bool possible1 = true, possible2 = true;
if (pos <= 0) {
possible = false;
possible1 = false;
}
if (!possible) {
possible1 = false;
possible2 = false;
}
for (int i = 0; i < pos && possible && possible1; i++) {
if (leftOver > 0) {
v.push_back(each + 1);
leftOver--;
} else {
v.push_back(each);
}
if (v[i] > n || v[i] <= 0) {
possible1 = false;
break;
}
}
int negVal = pos - n;
each = 0;
leftOver = 0;
if (neg > 0) {
each = negVal / neg;
leftOver = negVal % neg;
}
vector<int> v2;
if (neg == 0) possible2 = false;
for (int i = 0; i < neg && possible && !possible1 && possible2; i++) {
if (leftOver > 0) {
v2.push_back(each + 1);
leftOver--;
} else {
v2.push_back(each);
}
if (v2[i] > n || v2[i] <= 0) {
possible2 = false;
break;
}
}
int ptr = 0;
if (!possible) {
cout << "Impossible\n";
} else {
if (possible1 || possible2) {
cout << "Possible" << endl;
int current = 1;
for (int i = 0; i < s.length() && possible1; i++) {
if (s[i] == '-') {
current = 0;
} else if (s[i] == '+') {
current = 1;
}
if (s[i] != '?') {
cout << s[i];
} else {
if (current == 0) {
cout << 1;
} else {
cout << v[ptr];
ptr++;
}
}
}
ptr = 0;
current = 1;
for (int i = 0; i < s.length() && possible2 && !possible1; i++) {
if (s[i] == '-') {
current = 0;
} else if (s[i] == '+') {
current = 1;
}
if (s[i] != '?') {
cout << s[i];
} else {
if (current == 0) {
cout << v2[ptr];
ptr++;
} else {
cout << 1;
}
}
}
cout << "= " << n << endl;
} else
cout << "Impossible" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k = 0, n, pos = 1, neg = 0;
char s[100];
char c;
while (1) {
scanf(" %c", &c);
scanf(" %c", &c);
if (c == '=') break;
if (c == '+') pos++;
if (c == '-') neg++;
s[k++] = c;
}
cin >> n;
if (pos - n * neg > n || n * pos - neg < n) {
puts("Impossible");
return 0;
}
puts("Possible");
int S = 0;
for (int i = 0; i < k; i++) {
int sgn = 1;
if (i && s[i - 1] == '-') sgn = -1;
if (sgn == 1)
pos--;
else
neg--;
for (int x = 1; x <= n; x++) {
if (S + x * sgn + pos - n * neg <= n &&
S + x * sgn + n * pos - neg >= n) {
cout << x << " " << s[i] << " ";
S += x * sgn;
break;
}
}
}
cout << abs(n - S) << " = " << n << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
char mp[1000];
int add1[200];
int add2[200];
int a = 1, b = 0;
int n = 0;
int len;
bool slove() {
if (a - 1 >= b) {
if (a - 1 - n * b >= n) return false;
for (int i = 0; i < a - 1; i++) add1[i] = 1;
if (a - 1 - n * b >= 0) {
add1[a - 1] = n + n * b + 1 - a;
for (int i = 0; i < b; i++) add2[i] = n;
return true;
}
add1[a - 1] = n;
for (int i = 0; i < b; i++) add2[i] = 1;
int num = a - 1 - b;
for (int i = 0; i < b; i++)
if (num >= n - 1)
num -= n - 1, add2[i] = n;
else {
add2[i] = 1 + num;
return true;
}
} else {
for (int i = 0; i < b; i++) add2[i] = 1;
for (int i = 0; i < a; i++) add1[i] = 1;
int num = n + b - a;
if (num < 0) return false;
for (int i = 0; i < a; i++)
if (num >= n - 1)
num -= n - 1, add1[i] = n;
else {
add1[i] = 1 + num;
return true;
}
if (num > 0) return false;
}
}
bool prin() {
printf("Possible\n");
printf("%d", add1[0]);
int c = 1, d = 0;
for (int i = 1; i < len; i++)
if (mp[i] == '+') {
printf("+ %d", add1[c++]);
i += 2;
} else if (mp[i] == '-') {
printf("- %d", add2[d++]);
i += 2;
} else
printf("%c", mp[i]);
printf("\n");
}
int main() {
cin.getline(mp, 500);
len = strlen(mp);
int i = 0;
for (; i < len; i++)
if (mp[i] == '+')
a++;
else if (mp[i] == '-')
b++;
else if (mp[i] == '=')
break;
i += 2;
for (; i < len; i++) n = n * 10 + mp[i] - '0';
if (slove())
prin();
else
printf("Impossible\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int s[101], count = 1;
s[1] = 1;
int add = 1, sub = 0, n;
char c;
cin >> c;
while (c != '=') {
if (c == '+') {
add++;
s[++count] = 1;
} else if (c == '-') {
sub++;
s[++count] = 0;
}
cin >> c;
}
cin >> n;
if ((n > n * add - sub) || (n < add - n * sub)) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
int i, j, ans[count + 1];
for (i = 1; i <= count; i++) {
ans[i] = 1;
}
int total = add - sub;
while (total < n) {
for (i = 1; i <= count; i++) {
if (s[i] == 0 || ans[i] == n) continue;
while (total < n && ans[i] < n) {
total++;
ans[i]++;
}
}
}
while (total > n) {
for (i = 1; i <= count; i++) {
if (s[i] == 1 || ans[i] == n) continue;
while (total > n && ans[i] < n) {
total--;
ans[i]++;
}
}
}
for (i = 1; i <= count; i++) {
cout << ans[i] << " ";
if (i == count) continue;
if (s[i + 1] == 1)
cout << "+ ";
else
cout << "- ";
}
cout << "= " << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string S = "+";
int n;
while (1) {
string T;
cin >> T;
if (T == "+" || T == "-") {
S += T;
} else if (T == "=") {
cin >> n;
break;
}
}
int cnt = S.size();
int add = 0, sub = 0;
for (int i = 0; i < cnt; i++) {
if (S[i] == '+') {
add++;
}
if (S[i] == '-') {
sub--;
}
}
vector<int> L(cnt + 1), R(cnt + 1);
L[0] = 0;
R[0] = 0;
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (S[i] == '+') {
L[i + 1] = L[i] + 1;
R[i + 1] = R[i] + n;
}
if (S[i] == '-') {
L[i + 1] = L[i] - n;
R[i + 1] = R[i] - 1;
}
}
if (R[cnt] < n || n < L[cnt]) {
cout << "Impossible" << endl;
} else {
vector<int> num(cnt);
int value = n;
for (int i = cnt - 1; i >= 0; i--) {
for (int j = 1; j <= n; j++) {
if (S[i] == '+') {
if (L[i] <= value - j && value - j <= R[i]) {
num[i] = j;
value -= j;
break;
}
}
if (S[i] == '-') {
if (L[i] <= value + j && value + j <= R[i]) {
num[i] = j;
value += j;
break;
}
}
}
}
cout << "Possible" << endl;
for (int i = 0; i < cnt; i++) {
cout << num[i] << ' ';
if (i < cnt - 1) {
cout << S[i + 1] << ' ';
}
}
cout << "= " << n << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int p = 1, s = 0;
int sign[1000], ans[1000];
int ev(int i) { return sign[i] ? ans[i] : -ans[i]; }
int main() {
int cnt = 0;
char c;
int i = 1;
sign[0] = 1;
int sm = 0;
while (1) {
cin >> c;
cin >> c;
ans[cnt++] = 1;
sm += ev(cnt - 1);
if (c == '=') break;
p += (c == '+');
s += (c == '-');
sign[i++] = (c == '+');
}
int n;
cin >> n;
cerr << sm << endl;
for (int j = 0; j < int(cnt); ++j) {
if (sm > n && !sign[j]) {
ans[j] += min(n - 1, sm - n);
sm -= ans[j] - 1;
cerr << ans[j] << endl;
}
if (sm < n && sign[j]) {
ans[j] += min(n - 1, n - sm);
sm += ans[j] - 1;
}
cerr << j << ", sm = " << sm << endl;
}
if (sm == n) {
cout << "Possible\n";
} else {
cout << "Impossible\n";
return 0;
}
for (int i = 0; i < int(cnt); ++i) {
cout << ans[i];
if (i == cnt - 1)
cout << " = ";
else if (sign[i + 1])
cout << " + ";
else
cout << " - ";
}
cout << n << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<char> vec;
char c;
int n, nM = 0, nP = 0, d;
while (cin >> c && c != '=')
if (c == '+' || c == '-') {
vec.push_back(c);
if (c == '+') nP++;
if (c == '-') nM++;
}
d = nP - nM;
cin >> n;
if (n < nP + 1 - n * nM || n > n * (nP + 1) - nM) {
cout << "Impossible"
<< "\n";
return 0;
}
cout << "Possible"
<< "\n";
if (d == 0) {
cout << n << " ";
for (char c : vec) cout << c << " " << 1 << " ";
cout << "= " << n << "\n";
return 0;
}
if (d > 0) {
if (n - d <= 0) {
int reduce = d - n + 2;
cout << 1 << " ";
for (char c : vec) {
if (c == '+' || reduce <= 0)
cout << c << " " << 1 << " ";
else {
cout << c << " " << min(n, reduce) << " ";
reduce -= min(n, reduce) - 1;
}
}
} else {
cout << n - d << " ";
for (char c : vec) cout << c << " " << 1 << " ";
}
cout << "= " << n << "\n";
} else {
d *= -1;
if (n - d <= 0) {
int add = d + 1;
cout << n << " ";
for (char c : vec) {
if (c == '-' || add <= 0)
cout << c << " " << 1 << " ";
else {
cout << c << " " << min(n, add) << " ";
add -= min(n, add) - 1;
}
}
} else {
cout << n << " ";
bool sw = true;
for (char c : vec) {
if (c == '+' && sw) {
cout << c << " " << d + 1 << " ";
sw = false;
} else
cout << c << " " << 1 << " ";
}
}
cout << "= " << n << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int b[110];
int N = 1, A = 1, B = 0;
b[0] = 1;
while (1) {
char q[2], s[2];
scanf("%s%s", q, s);
if (s[0] == '+') {
b[N] = 1;
N++;
A++;
} else if (s[0] == '-') {
b[N] = -1;
N++;
B++;
} else {
break;
}
}
scanf("%d", &n);
if (A * 1 - B * n > n || A * n - B * 1 < n) {
puts("Impossible");
} else {
puts("Possible");
int C[110];
int t = n;
for (int i = 0; i < N; i++) {
if (b[i] == 1) {
A--;
C[i] = min(n, t - (A - B * n));
t -= C[i];
} else {
B--;
C[i] = max(1, (A * 1 - B * n) - t);
t += C[i];
}
}
for (int i = 0; i < N; i++) {
if (i > 0) {
printf(" %c ", b[i] == 1 ? '+' : '-');
}
printf("%d", C[i]);
}
printf(" = %d\n", n);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
string t = "+";
int n = 0, p = 0, m = 0;
string s;
vector<int> P;
while (cin >> s) {
if (s == "?") {
if (t == "+")
p++, P.push_back(0);
else
m++, P.push_back(1);
} else if (s == "+" || s == "-")
t = s;
else
n = atoi(s.c_str());
}
if (p - n * m > n || n > n * p - m)
cout << "Impossible\n";
else {
cout << "Possible\n";
int xm = 1, xp = 1, S = 0;
for (int i = 0; i < P.size(); ++i) {
if (P[i]) {
m--;
while (S - xm + p - n * m > n || n > S - xm + n * p - m) xm++;
cout << xm << " ";
S -= xm;
} else {
p--;
while (S + xp + p - n * m > n || n > S + xp + n * p - m) xp++;
cout << xp << " ";
S += xp;
}
if (i == P.size() - 1) break;
if (P[i + 1] == 0)
cout << "+ ";
else
cout << "- ";
}
cout << "= " << n << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int minv(int plus, int minus, int n) { return plus - minus * n; }
int maxv(int plus, int minus, int n) { return plus * n - minus; }
int main() {
ios_base::sync_with_stdio(0);
string s;
getline(cin, s);
int plus = 0, minus = 0, n = 0;
char prev = '+';
vector<int> order;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == ' ')
continue;
else if (s[i] == '+' || s[i] == '-')
prev = s[i];
else if (s[i] == '?') {
if (prev == '+')
plus++, order.push_back(1);
else
minus++, order.push_back(0);
} else if (isdigit(s[i])) {
n *= 10;
n += s[i] - '0';
}
}
if (maxv(plus, minus, n) < n || minv(plus, minus, n) > n) {
cout << "Impossible";
return 0;
}
cout << "Possible\n";
int cur = 0;
vector<int> p, m;
while (plus > 0) {
for (int i = 1; i < n + 1; i++) {
cur += i;
if (minv(plus - 1, minus, n) + cur > n ||
maxv(plus - 1, minus, n) + cur < n) {
cur -= i;
} else {
p.push_back(i);
break;
}
}
plus--;
}
while (minus > 0) {
for (int i = 1; i < n + 1; i++) {
cur -= i;
if (minv(plus, minus - 1, n) + cur > n ||
maxv(plus, minus - 1, n) + cur < n) {
cur += i;
} else {
m.push_back(i);
break;
}
}
minus--;
}
for (int i = 0; i < (int)order.size(); i++) {
if (order[i] == 1) {
if (i != 0) cout << " + ";
cout << p.back();
p.pop_back();
} else {
cout << " - ";
cout << m.back();
m.pop_back();
}
}
cout << " = " << n;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x, y, nrm, nrp, nr;
int pls[500], mns[500];
char sir[1000000];
bool verif(int v1) {
int v2 = v1 - nr;
int lmin = y;
int lmax = y * nr;
if (v2 <= lmax && v2 >= lmin) {
return 1;
}
return 0;
}
void afis(int v1) {
int v2 = v1 - nr;
int sign;
for (int i = 1; i <= x; ++i) {
pls[i] = v1 / x;
if (i <= v1 % x) {
++pls[i];
}
}
for (int i = 1; i <= y; ++i) {
mns[i] = v2 / y;
if (i <= v2 % y) {
++mns[i];
}
}
if (sir[0] == '-') {
sign = -1;
} else {
sign = 1;
}
for (int i = 0; i < n; ++i) {
if (sir[i] == '?') {
if (sign == 1) {
cout << pls[++nrp] << " ";
} else {
cout << mns[++nrm] << " ";
}
} else {
cout << sir[i];
}
if (sir[i] == '+') {
cout << " ";
sign = 1;
}
if (sir[i] == '-') {
cout << " ";
sign = -1;
}
if (sir[i] == '=') {
cout << " ";
}
}
}
int main() {
char ch;
while (cin >> ch) {
sir[n] = ch;
++n;
}
if (sir[0] == '-') {
++y;
} else {
++x;
}
for (int i = 1; i < n; ++i) {
if (sir[i] == '+') {
++x;
}
if (sir[i] == '-') {
++y;
}
while (sir[i] >= '0' && sir[i] <= '9' && i < n) {
nr = nr * 10 + sir[i] - '0';
++i;
}
}
int aux1 = x;
int aux2 = x * nr;
for (int i = aux1; i <= aux2; ++i) {
if (verif(i)) {
cout << "Possible\n";
afis(i);
return 0;
}
}
cout << "Impossible\n";
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
int main() {
string s;
vector<int> v;
bool plus = true;
while (cin >> s) {
if (s == "=") {
break;
}
if (s == "-") plus = false;
if (s == "+") plus = true;
if (s == "?") v.push_back(plus ? 1 : -1);
}
int n;
cin >> n;
int np = 0;
int nm = 0;
for (int i = (0); i < (v.size()); i++)
if (v[i] == 1)
np++;
else
nm++;
int from = np;
int to = np * n;
from -= nm * n;
to -= nm;
if (from <= n && n <= to) {
puts("Possible");
int sum = 0;
for (int i = (0); i < (v.size()); i++) {
if (v[i] > 0)
np--;
else
nm--;
if (i) printf(" %c ", v[i] == 1 ? '+' : '-');
for (int j = 1; j <= n; j++) {
from = sum + v[i] * j + np;
to = sum + v[i] * j + np * n;
from -= nm * n;
to -= nm;
if (from <= n && n <= to) {
cout << j;
sum += j * v[i];
break;
}
}
}
cout << " = " << n << endl;
} else {
puts("Impossible");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long lastnb(string s) {
int i = s.size() - 1, n = 0;
while (isdigit(s[i])) i--;
i++;
while (i < s.size()) {
n = n * 10 + s[i] - '0';
i++;
}
return n;
}
int main() {
string s;
getline(cin, s);
long long n = lastnb(s);
long long cntminus = 0, cntplus = 1;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == '+')
cntplus++;
else if (s[i] == '-')
cntminus++;
}
if (cntplus * n - cntminus < n || cntplus - n * cntminus > n) {
cout << "Impossible";
} else {
cout << "Possible\n";
if (cntplus - cntminus >= n) {
long long target = cntplus - n;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == '?') {
if (i == 0 || s[i - 2] == '+')
cout << 1;
else {
cout << min(n, target - cntminus + 1);
target -= min(n, target - cntminus + 1);
cntminus--;
}
} else {
cout << s[i];
}
}
} else {
long long target = cntminus + n;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == '?') {
if (i != 0 && s[i - 2] == '-')
cout << 1;
else {
cout << min(n, target - cntplus + 1);
target -= min(n, target - cntplus + 1);
cntplus--;
}
} else {
cout << s[i];
}
}
}
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.