#include<iostream> usingnamespace std; // https://vjudge.net/contest/505193#problem/A int z; longlongfast_power(longlong a, longlong b, longlong c) { longlong ans = 1; a %= c; while (b) { if (b % 2) { ans = (ans * a) % c; } b /= 2; a = (a * a) % c; } return ans; } intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> z; while (z--) { longlong ans = 0; int M, H; cin >> M >> H; for (int i = 1; i <= H; i++) { longlong a, b; cin >> a >> b; ans += fast_power(a, b, M); } cout << ans % M << endl; } return0; }
#include<bits/stdc++.h> usingnamespace std; // https://vjudge.net/contest/505193#problem/B // 位运算(每一位分别考虑) int n, m; structnode { string op; int t; } door[110000]; intcal(int tar, int bit) { for (int i = 1; i <= n; i++) { int tar2 = door[i].t >> bit & 1; //取从右往左第bit位(从0开始) if (door[i].op == "AND") tar = tar2 & tar; if (door[i].op == "XOR") tar = tar2 ^ tar; if (door[i].op == "OR") tar = tar2 | tar; } return tar; } intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> door[i].op >> door[i].t; } // int limit = 0; // while (m) // { // m = m >> 1; // limit++; // } //求m的最高位数 int ans = 0; int tarm = 0; for (int bit = 30; bit >= 0; bit--) { int res0 = cal(0, bit); //若bit位填0 int res1 = cal(1, bit); //若bit位填1 int m0 = tarm; int m1 = tarm + (1 << bit); if (m1 <= m && res1 > res0) { ans += res1 << bit; tarm = m1; } if (res0 >= res1) { ans += res0 << bit; } } cout << ans; return0; }
#include<iostream> #include<map> usingnamespace std; // https://vjudge.net/contest/505193#problem/D int N, I, H, R; int cf[10010]; int a[10010]; map<int, bool> visit[10000]; intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> N >> I >> H >> R; for (int i = 1; i <= R; i++) { int a, b; cin >> a >> b; // a与b可以互相看见,说明下标a+1到下标b-1都要减去1 if (a > b) swap(a, b); if (visit[a][b]) continue; cf[a + 1]--; cf[b]++; visit[a][b] = true; } for (int i = 1; i <= N; i++) { a[i] = a[i - 1] + cf[i]; cout << a[i] + H << endl; } return0; }
#include<bits/stdc++.h> usingnamespace std; //https://vjudge.net/contest/505193#problem/F int n, m; int a[10000000]; int cf[10000000]; int temp[10000000]; structnode { int num; int s; int e; } ask[10000000]; boolcheck(int ans) { memset(temp, 0, sizeof(temp)); memset(cf, 0, sizeof(cf)); for (int i = 1; i <= ans; i++) { int s = ask[i].s; int e = ask[i].e; int n = ask[i].num; cf[s] += n; cf[e + 1] -= n; } for (int i = 1; i <= n; i++) { temp[i] = temp[i - 1] + cf[i]; if (temp[i] > a[i]) returntrue; } returnfalse; } intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= m; i++) { cin >> ask[i].num >> ask[i].s >> ask[i].e; } int l = 1, r = m; if (!check(m)) { cout << 0 << endl; return0; } int ans = -1; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } cout << -1 << endl; cout << ans; return0; }
#include<bits/stdc++.h> usingnamespace std; // https://www.luogu.com.cn/problem/P1031 int n, a[10010]; intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } int aver = sum / n; for (int i = 1; i <= n; i++) a[i] -= aver; int ans = 0; for (int i = 1; i <= n; i++) { // cout << a[i] << endl; if (a[i] != 0) { a[i + 1] += a[i]; a[i] = 0; ans++; } } cout << ans << endl; return0; }
#include<iostream> #include<algorithm> usingnamespace std; // 传送门:https://www.luogu.com.cn/problem/P2512 // 题解:https://www.luogu.com.cn/problem/solution/P2512 // 中位数! int m; longlong a[1000010]; longlong c[1000010]; intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> m; longlong aver = 0, ans = 0; for (int i = 1; i <= m; i++) { cin >> a[i]; aver += a[i]; } aver /= m; for (int i = 1; i <= m; i++) { c[i] = c[i - 1] + aver - a[i - 1]; } sort(c + 1, c + 1 + m); int mid = c[(1 + m) >> 1]; for (int i = 1; i <= m; i++) { ans += abs(mid - c[i]); } cout << ans; return0; }