C - Make Takahashi Happy (atcoder.jp)
蛮板的dfs,但是不板的我也写不出来。
#include <bits/stdc++.h>
using namespace std;
#define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
#define i64 __int64
//#define int ll
#define pb push_back
#define eb emplace_back
#define m_p make_pair
#define mod 998244353
#define mem(a,b) memset(a,b,sizeof a)
#define pii pair<int,int>
#define fi first
#define inf 0x3f3f3f3f
const int N = 3e5 + 50;
//__builtin_ctzll(x);后导0的个数
//__builtin_popcount计算二进制中1的个数
int a[20][20];
int n, m;
int cnt = 0;
void dfs(int x, int y, set<int>se) {
if (x == n && y == m) {
cnt++;
}
if (x < n) {
x++;
if (!se.count(a[x][y])) {
se.insert(a[x][y]);
dfs(x, y, se);
se.erase(a[x][y]);
}
x--;
}
if (y < m) {
y++;
if (!se.count(a[x][y])) {
se.insert(a[x][y]);
dfs(x, y, se);
se.erase(a[x][y]);
}
y--;
}
}
void work() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
set<int>ss;
ss.insert(a[1][1]);
dfs(1, 1, ss);
cout << cnt << '\n';
}
signed main() {
io;
work();
return 0;
}
思路:
并查集判环,我之前一直觉得并查集只能判连通块,原来如果f连的好并查集是可以判有向环的,更喜欢了。然后开两个set塞所有父节点(即连通块),记录了成环的剩下的就是不成环的。
#include <bits/stdc++.h>
using namespace std;
#define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
#define i64 __int64
//#define int ll
#define pb push_back
#define eb emplace_back
#define m_p make_pair
#define mod 998244353
#define mem(a,b) memset(a,b,sizeof a)
#define pii pair<int,int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 3e5 + 50;
//__builtin_ctzll(x);后导0的个数
//__builtin_popcount计算二进制中1的个数
int f[N];
int n, m;
bool vis[N];
void init() {
for (int i = 1; i <= n; ++i) {
f[i] = i;
}
}
int fa(int a) {
if (f[a] == a)
return a;
return f[a] = fa(f[a]);
}
void work() {
cin >> n >> m;
init();
while (m--) {
int a, b;
char c, d;
cin >> a >> c >> b >> d;
if (fa(a) != fa(b)) {
f[fa(a)] = fa(b);
} else {
vis[fa(a)] = 1;
}
}
set<int>s1, s2;
for (int i = 1; i <= n; ++i) {
if (vis[fa(i)])
s1.insert(fa(i));
s2.insert(fa(i));
}
cout << s1.size() << " " << s2.size() - s1.size() << '\n';
}
signed main() {
io;
work();
return 0;
}
E - Geometric Progression (atcoder.jp)
思路:
这什么因式分解递归题啊。
注意到当x为偶数的时候,令k=x-1为奇数,1+a^1+……+a^k = (1+a^(k/2+1))*(1+a^1+……+a^(k/2)), 相当于把前一半和后一半提出公因式乘1和x^(k/2),然后可以发现这个公因数就是原式的一半的形态,可以这样递归下去。当x为奇数的时候我们就把a^k单拎出来求qp,剩下的按照偶数去递归。(有点qp的意思哦)
#include <bits/stdc++.h>
using namespace std;
#define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
#define i64 __int64
//#define int ll
#define pb push_back
#define eb emplace_back
#define m_p make_pair
//#define mod 998244353
#define mem(a,b) memset(a,b,sizeof a)
#define pii pair<int,int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 3e5 + 50;
//__builtin_ctzll(x);后导0的个数
//__builtin_popcount计算二进制中1的个数
ll mod;
ll qp(ll a, ll b) {
ll ans = 1;
a %= mod;
while (b) {
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll cal(ll a, ll x) {
if (x == 1)
return 1 % mod;
if (x & 1) {
return (qp(a, x - 1) + cal(a, x - 1) ) % mod;
} else
return (1 + qp(a, x / 2)) % mod * cal(a, x / 2) % mod;
}
void work() {
ll a, x;
cin >> a >> x >> mod;
ll ans = cal(a, x);
cout << ans << '\n';
}
signed main() {
io;
work();
return 0;
}
没啥好说的,好板的莫队。
#include <bits/stdc++.h>
using namespace std;
#define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
#define int ll
#define pb push_back
#define eb emplace_back
#define m_p make_pair
#define mod 998244353
#define mem(a,b) memset(a,b,sizeof a)
#define pii pair<int,int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 3e5 + 50;
//__builtin_ctzll(x);后导0的个数
//__builtin_popcount计算二进制中1的个数
ll cnt[N], ans[N];
int b[N], n, m, k, res;
struct Query {
int l, r, id, pos;
bool operator <(const Query &x) {
if (pos == x.pos)
return r < x.r;
else
return pos < x.pos;
}
} a[N];
void add(int x) {
cnt[b[x]]++;
if (cnt[b[x]] >= 3)
res += 1ll * (cnt[b[x]] - 1 ) * (cnt[b[x]] - 2) / 2;
}
void del(int x) {
if (cnt[b[x]] >= 3)
res -= (cnt[b[x]] - 1 ) * (cnt[b[x]] - 2) / 2;
cnt[b[x]]--;
}
void work() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> b[i];
}
int size = sqrt(n);
for (int i = 1; i <= m; ++i) {
cin >> a[i].l >> a[i].r;
a[i].id = i;
a[i].pos = (a[i].l - 1) / size + 1;
}
sort(a + 1, a + 1 + m);
int l = 1, r = 0;
for (int i = 1; i <= m; ++i) {
while (l > a[i].l) {
add(--l);
}
while (r < a[i].r) {
add(++r);
}
while (l < a[i].l) {
del(l++);
}
while (r > a[i].r) {
del(r--);
}
ans[a[i].id] = res;
}
for (int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
}
signed main() {
io;
work();
return 0;
}