mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-21 06:31:43 +00:00
Compare commits
4 Commits
8f1e2bd8c2
...
50a8547e70
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50a8547e70 | ||
|
|
993b5c017b | ||
|
|
dc42ece1aa | ||
|
|
9ddad129d6 |
63
src/8/11/P11361.cpp
Normal file
63
src/8/11/P11361.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
using ll = int64_t;
|
||||
|
||||
static inline void solve() {
|
||||
int n;
|
||||
std::cin >> n;
|
||||
std::string s1, s2, t1, t2;
|
||||
std::cin >> s1 >> s2 >> t1 >> t2;
|
||||
|
||||
ll ans = 0;
|
||||
std::vector<bool> p(n, false);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (t1[i] == '0' && t2[i] == '0') {
|
||||
if (s1[i] == s2[i]) {
|
||||
ans++;
|
||||
}
|
||||
p[i] = true;
|
||||
}
|
||||
}
|
||||
int idx = 0;
|
||||
while (idx < n) {
|
||||
while (idx < n && (p[idx] || t1[idx] == '0' || t2[idx] == '0')) {
|
||||
idx++;
|
||||
}
|
||||
if (idx >= n) {
|
||||
break;
|
||||
}
|
||||
int s = idx;
|
||||
int e = s;
|
||||
while (e + 1 < n && !p[e + 1] && t1[e + 1] == '1' && t2[e + 1] == '1') {
|
||||
e++;
|
||||
}
|
||||
int s10 = 0, s11 = 0;
|
||||
int s20 = 0, s21 = 0;
|
||||
for (int i = s; i <= e; ++i) {
|
||||
if (s1[i] == '0') s10++;
|
||||
else s11++;
|
||||
|
||||
if (s2[i] == '0') s20++;
|
||||
else s21++;
|
||||
}
|
||||
ans += std::min(s10, s20) + std::min(s11, s21);
|
||||
idx = e + 1;
|
||||
}
|
||||
std::cout << ans << "\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios_base::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::cout.tie(nullptr);
|
||||
int T;
|
||||
std::cin >> T;
|
||||
while (T--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
84
src/8/11/P11362.cpp
Normal file
84
src/8/11/P11362.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
using ll = long long;
|
||||
using namespace std;
|
||||
const int MOD = 1e9 + 7;
|
||||
|
||||
ll p(ll b, ll e) {
|
||||
ll res = 1;
|
||||
b %= MOD;
|
||||
while (e > 0) {
|
||||
if (e % 2 == 1) res = (res * b) % MOD;
|
||||
b = (b * b) % MOD;
|
||||
e /= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
ll n;
|
||||
int m;
|
||||
ll v;
|
||||
cin >> n >> m >> v;
|
||||
map<int, int> mc;
|
||||
for (int i = 0; i < m; ++i) {
|
||||
int c, d;
|
||||
cin >> c >> d;
|
||||
if (mc.count(c) && mc[c] != d) {
|
||||
for (int j = i + 1; j < m; ++j) cin >> c >> d;
|
||||
cout << 0 << endl;
|
||||
return;
|
||||
}
|
||||
mc[c] = d;
|
||||
}
|
||||
if (m == 0) {
|
||||
if (n == 1) {
|
||||
cout << 1 << endl;
|
||||
} else {
|
||||
cout << p(v, (n - 1) * 2) << endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
ll tot = 1;
|
||||
ll vmod = v % MOD;
|
||||
int lp = 0;
|
||||
for (auto const& [idx, val] : mc) {
|
||||
ll len = idx - lp;
|
||||
if (len > 0) {
|
||||
ll s;
|
||||
if (lp == 0) {
|
||||
s = p(vmod, (len - 1) * 2);
|
||||
} else {
|
||||
ll v2len = p(vmod, len * 2);
|
||||
ll vlen = p(vmod, len);
|
||||
ll vlenm1 = p(vmod, len - 1);
|
||||
s = (v2len - vlen + vlenm1 + MOD) % MOD;
|
||||
}
|
||||
tot = (tot * s) % MOD;
|
||||
}
|
||||
lp = idx;
|
||||
}
|
||||
|
||||
|
||||
if (lp < n) {
|
||||
ll len = n - lp;
|
||||
|
||||
|
||||
ll s = p(vmod, len * 2);
|
||||
tot = (tot * s) % MOD;
|
||||
}
|
||||
|
||||
cout << tot << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios_base::sync_with_stdio(false);
|
||||
cin.tie(NULL);
|
||||
cout.tie(nullptr);
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
50
src/8/11/P11363.cpp
Normal file
50
src/8/11/P11363.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using ll = int64_t;
|
||||
|
||||
ll n,k,T,e;
|
||||
std::vector<std::vector<ll>> map;
|
||||
std::vector<ll> d,jc;
|
||||
const ll M = 1e9+7;
|
||||
|
||||
void solve(){
|
||||
std::cin>>n>>k;
|
||||
map.clear();
|
||||
map.resize(n+1);
|
||||
d.clear();
|
||||
d.resize(n+1);
|
||||
for(ll i=1;i<=n-1;i++){
|
||||
ll u,v;
|
||||
std::cin>>u>>v;
|
||||
map[u].push_back(v);
|
||||
map[v].push_back(u);
|
||||
d[u]++;
|
||||
d[v]++;
|
||||
}
|
||||
std::cin>>e;
|
||||
ll maxd=1;
|
||||
for(ll i=1;i<=n-1;i++){
|
||||
maxd=std::max(maxd,d[i]);
|
||||
}
|
||||
jc.clear();
|
||||
jc.resize(maxd+1);
|
||||
jc[1]=1;
|
||||
for(ll i=2;i<=maxd;i++){
|
||||
jc[i]=jc[i-1]*i%M;
|
||||
}
|
||||
ll ans=1;
|
||||
for(ll i=1;i<=n-1;i++){
|
||||
ans=(ans+jc[d[i]-1])%M;
|
||||
}
|
||||
std::cout<<(ans*2)%M<<'\n';
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::cin>>T;
|
||||
while(T--){
|
||||
solve();
|
||||
}
|
||||
}
|
||||
21
src/8/11/P11364.cpp
Normal file
21
src/8/11/P11364.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
using ll = int64_t;
|
||||
|
||||
ll n;
|
||||
std::vector<std::vector<ll>> m;
|
||||
|
||||
int main(){
|
||||
std::cin>>n;
|
||||
m.resize(n+1);
|
||||
for(ll i=1;i<=n-1;i++){
|
||||
ll u,v;
|
||||
std::cin>>u>>v;
|
||||
m[u].push_back(v);
|
||||
m[v].push_back(u);
|
||||
}
|
||||
|
||||
}
|
||||
39
src/8/11/longest-substring-without-repeating-characters.cpp
Normal file
39
src/8/11/longest-substring-without-repeating-characters.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int lengthOfLongestSubstring(std::string s) {
|
||||
|
||||
std::vector<int> last_pos(128, -1);
|
||||
int n = s.length();
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
int max_len = 0;
|
||||
int left = 0;
|
||||
|
||||
for (int right = 0; right < n; ++right) {
|
||||
|
||||
char current_char = s[right];
|
||||
|
||||
if (last_pos[current_char] >= left) {
|
||||
left = last_pos[current_char] + 1;
|
||||
}
|
||||
|
||||
last_pos[current_char] = right;
|
||||
|
||||
max_len = std::max(max_len, right - left + 1);
|
||||
}
|
||||
return max_len;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
}
|
||||
35
src/8/11/range-product-queries-of-powers.cpp
Normal file
35
src/8/11/range-product-queries-of-powers.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
using ll = int64_t;
|
||||
public:
|
||||
static inline ll lowbit(ll n){
|
||||
return n&(-n);
|
||||
}
|
||||
constexpr vector<int> productQueries(int n, vector<vector<int>>& queries) {
|
||||
const ll M=1e9+7;
|
||||
vector<ll> v;
|
||||
v.reserve(128);
|
||||
while(lowbit(n)){
|
||||
v.push_back(lowbit(n));
|
||||
n-=lowbit(n);
|
||||
}
|
||||
vector<int> ans;
|
||||
ans.reserve(queries.size());
|
||||
for(ll i=0;i<queries.size();i++){
|
||||
ll nans=1;
|
||||
for(ll j=queries[i][0];j<=queries[i][1];j++){
|
||||
nans=nans*v[j]%M;
|
||||
}
|
||||
ans.push_back(nans);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
}
|
||||
23
src/8/11/two-sum.cpp
Normal file
23
src/8/11/two-sum.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
unordered_map<int, int> m;
|
||||
for(int i=0;i<nums.size();i++){
|
||||
if(auto p = m.find(target-nums[i]);p!=m.end()){
|
||||
return {p->second,i};
|
||||
}
|
||||
m.insert({nums[i],i});
|
||||
}
|
||||
return {0,0};
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
|
||||
}
|
||||
@ -104,13 +104,10 @@ int main() {
|
||||
// 题目输入为文件,实际比赛中需要取消注释
|
||||
// freopen("maze.in", "r", stdin);
|
||||
// freopen("maze.out", "w", stdout);
|
||||
|
||||
FAST_IO;
|
||||
|
||||
std ::ios_base ::sync_with_stdio(false);
|
||||
std ::cin.tie(0);
|
||||
std::cin >> T;
|
||||
while (T--) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user