1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <algorithm> #include <cstdio> #include <cstring> #include <vector> #define ll long long int n, m; std::vector<int> k, p; inline int mpow(int x, int k) { int ans = 1; for (; k; k >>= 1, x = x * x) { if (k & 1) ans = ans * x; } return ans; } std::vector<int> ans1, ans2; int cnt1, cnt2; inline void dfs(std::vector<int>& ans, int& cnt, int l, int r, int v) { if (l > r) { ++cnt; ans.push_back(v); return; } for (int i = 1; i <= m; ++i) { dfs(ans, cnt, l + 1, r, v + k[l] * mpow(i, p[l])); } return; } std::vector<int> cntans1; int main() { scanf("%d%d", &n, &m); k.resize(n + 1); p.resize(n + 1); for (int i = 1; i <= n; ++i) { scanf("%d%d", &k[i], &p[i]); } dfs(ans1, cnt1, 1, n >> 1, 0); dfs(ans2, cnt2, (n >> 1) + 1, n, 0); std::sort(ans1.begin(), ans1.end()); int newcnt1 = 1; cntans1.push_back(1); for (int i = 1; i < cnt1; ++i) { if (ans1[i] == ans1[newcnt1 - 1]) { ++cntans1[newcnt1 - 1]; } else { ans1[newcnt1++] = ans1[i]; cntans1.push_back(1); } } cnt1 = newcnt1; std::sort(ans2.begin(), ans2.end()); int las = 0; ll ans = 0; for (int i = cnt2 - 1; i >= 0; --i) { for (; las < cnt1 && ans1[las] + ans2[i] < 0; ++las) ; if (las < cnt1 && ans1[las] + ans2[i] == 0) ans += cntans1[las]; } printf("%lld\n", ans); return 0; } |
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、删除第 51 行的 std::sort(ans2.begin(), ans2.end()); 后,代码输出的结果不会受到影响。
2、假设计算过程中不发生溢出,函数 mpow(x,k) 的功能是求出 xkxk 的取值。()
3、代码中第 39 行到第 50 行的目的是为了将 ans1 数组进行“去重”操作。()
4、当输入为“3 1 5 1 2 -1 2 1”时,输出结果为
5、记程序结束前p数组元素的最大值为P,则该代码的时间复杂度是
6、本题所求出的是( )。
