Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92c6fc6

Browse files
Some DMOJ problems
1 parent 8220d2f commit 92c6fc6

File tree

4 files changed

+344
-0
lines changed

4 files changed

+344
-0
lines changed

‎DMOJ/coci15c4p5.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
6+
const int MAXN = 1e5 + 10;
7+
8+
vector<int> grafo[MAXN];
9+
map<int,ll> freq[MAXN];
10+
int xorsum[MAXN],pai[MAXN],e1[MAXN],e2[MAXN],e3[MAXN],tamanho[MAXN],tira[MAXN],N,K;
11+
ll total,exibe[MAXN];
12+
13+
int find(int x){
14+
if(x == pai[x]) return x;
15+
return pai[x] = find(pai[x]);
16+
}
17+
18+
void join(int x,int y){
19+
x = find(x);
20+
y = find(y);
21+
if(x == y) return;
22+
if(tamanho[x] < tamanho[y]) swap(x,y);
23+
tamanho[x] += tamanho[y];
24+
pai[y] = x;
25+
for(map<int,ll>::iterator it = freq[y].begin();it != freq[y].end();it++){
26+
ll v1 = (*it).first, v2 = (*it).second;
27+
if(freq[x].count(v1)) total += freq[x][v1]*v2;
28+
}
29+
for(map<int,ll>::iterator it = freq[y].begin();it != freq[y].end();it++){
30+
ll v1 = (*it).first, v2 = (*it).second;
31+
freq[x][v1] += v2;
32+
}
33+
}
34+
35+
void dfs(int v,int p){
36+
for(int idx : grafo[v]){
37+
int u = (e1[idx] != v) ? (e1[idx]) : (e2[idx]),w = e3[idx];
38+
if(u == p) continue;
39+
xorsum[u] = xorsum[v] ^ w;
40+
dfs(u,v);
41+
}
42+
tamanho[v] = 1;
43+
pai[v] = v;
44+
freq[v][xorsum[v]]++;
45+
}
46+
47+
int main(){
48+
scanf("%d",&N);
49+
for(int i = 1;i<N;i++){
50+
scanf("%d %d %d",&e1[i],&e2[i],&e3[i]);
51+
grafo[e1[i]].push_back(i);
52+
grafo[e2[i]].push_back(i);
53+
}
54+
for(int i = 1;i<N;i++) scanf("%d",&tira[i]);
55+
dfs(1,-1);
56+
exibe[N] = 0;
57+
for(int i = N-1;i>=1;i--){
58+
int j = tira[i];
59+
join(e1[j],e2[j]);
60+
exibe[i] = total;
61+
}
62+
for(int i = 1;i<=N;i++) printf("%lld\n",exibe[i]);
63+
return 0;
64+
}

‎DMOJ/coci16c1p3.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
5+
const int MAXN = 1e3 + 10;
6+
7+
string S[MAXN];
8+
vector<char> R[MAXN];
9+
char vai[MAXN],atual;
10+
int vetor[MAXN],grau[MAXN],N,possivel;
11+
12+
int ehprefixo(int a,int b){
13+
if(S[a].size() >= S[b].size()) return 0;
14+
for(int i = 0;i<S[a].size();i++){
15+
if(S[a][i] != S[b][i]){
16+
return 0;
17+
}
18+
}
19+
return 1;
20+
}
21+
22+
int checa(int a,int b){
23+
if(ehprefixo(a,b)) return 1;
24+
if(ehprefixo(b,a)) return 0;
25+
for(int i = 0;i<S[a].size() && i < S[b].size();i++){
26+
if(S[a][i] != S[b][i]){
27+
R[S[a][i]].push_back(S[b][i]);
28+
grau[S[b][i]]++;
29+
break;
30+
}
31+
}
32+
return 1;
33+
}
34+
35+
int main(){
36+
atual = 'a';
37+
possivel = 1;
38+
cin >> N;
39+
for(int i = 1;i<=N;i++){
40+
cin >> S[i];
41+
}
42+
for(int i = 1;i<=N;i++){
43+
cin >> vetor[i];
44+
}
45+
for(int i = 1;i<=N;i++){
46+
for(int j = i+1;j<=N;j++){
47+
if(checa(vetor[i],vetor[j]) == 0){
48+
possivel = 0;
49+
}
50+
}
51+
}
52+
queue<char> bfs;
53+
for(char c = 'a';c<='z';c++){
54+
if(grau[c] == 0) bfs.push(c);
55+
}
56+
while(!bfs.empty()){
57+
char v = bfs.front();
58+
//cout << v << endl;
59+
bfs.pop();
60+
vai[v] = atual;
61+
atual++;
62+
for(char u : R[v]){
63+
grau[u]--;
64+
if(grau[u] == 0) bfs.push(u);
65+
}
66+
}
67+
for(char c = 'a';c <= 'z';c++){
68+
if(grau[c] != 0){
69+
possivel = 0;
70+
}
71+
}
72+
if(!possivel){
73+
cout << "NE" << endl;
74+
}
75+
else{
76+
cout << "DA" << endl;
77+
for(char c = 'a';c<='z';c++) cout << vai[c];
78+
cout << endl;
79+
}
80+
return 0;
81+
}

‎DMOJ/ddrp6.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
typedef tuple<char,char,char,char> quadra;
6+
typedef tuple<char,char,char> trinca;
7+
8+
const int MAXN = 64;
9+
const ll MOD = 1e9 + 7;
10+
char possiveis[4] = {'L','R','U','D'};
11+
12+
vector<trinca> todas;
13+
map<trinca,int> conversao;
14+
quadra proibida1,proibida2;
15+
16+
struct matrix{
17+
18+
ll mat[MAXN][MAXN];
19+
20+
matrix(int identidade = 0){
21+
for(int i = 0;i<MAXN;i++){
22+
for(int j = 0;j<MAXN;j++){
23+
mat[i][j] = ((i == j) && identidade);
24+
}
25+
}
26+
}
27+
28+
matrix operator*(const matrix& other)const{
29+
matrix ans;
30+
for(int i = 0;i<MAXN;i++){
31+
for(int j = 0;j<MAXN;j++){
32+
for(int k = 0;k<MAXN;k++){
33+
ans.mat[i][j] = (ans.mat[i][j] + mat[i][k]*other.mat[k][j]) % MOD;
34+
}
35+
}
36+
}
37+
return ans;
38+
}
39+
40+
};
41+
42+
matrix expo(ll pot,matrix A){
43+
matrix resposta(1);
44+
matrix copia = A;
45+
for(ll i = 0;(1LL << i) <= pot;i++){
46+
if(pot & (1LL << i)) resposta = resposta*copia;
47+
copia = copia*copia;
48+
}
49+
return resposta;
50+
}
51+
52+
quadra rotaciona(quadra q){
53+
return make_tuple(get<3>(q),get<0>(q),get<1>(q),get<2>(q));
54+
}
55+
56+
quadra adiciona(trinca t,char letra){
57+
return make_tuple(get<0>(t),get<1>(t),get<2>(t),letra);
58+
}
59+
60+
trinca ultimas(quadra q){
61+
return make_tuple(get<1>(q),get<2>(q),get<3>(q));
62+
}
63+
64+
int get_id(trinca t){
65+
if(!conversao.count(t)){
66+
int tam = conversao.size();
67+
conversao[t] = tam;
68+
todas.push_back(t);
69+
}
70+
return conversao[t];
71+
}
72+
73+
int main(){
74+
75+
ll N;
76+
cin >> N;
77+
78+
if(N < 3){
79+
if(N == 1) cout << 4 << endl;
80+
else cout << 16 << endl;
81+
return 0;
82+
}
83+
84+
proibida1 = make_tuple('L','D','R','U');
85+
proibida2 = make_tuple('L','U','R','D');
86+
matrix base;
87+
88+
for(int i = 0;i<4;i++){
89+
for(int j = 0;j<4;j++){
90+
for(int k = 0;k<4;k++){
91+
trinca davez = make_tuple(possiveis[i],possiveis[j],possiveis[k]);
92+
int numero = get_id(davez);
93+
}
94+
}
95+
}
96+
97+
for(trinca davez : todas){
98+
for(int i = 0;i<4;i++){
99+
quadra nova = adiciona(davez,possiveis[i]);
100+
int flag = 0;
101+
quadra copia1 = proibida1,copia2 = proibida2;
102+
for(int j = 0;j<=4;j++){
103+
if(copia1 == nova || copia2 == nova) flag = 1;
104+
copia1 = rotaciona(copia1);
105+
copia2 = rotaciona(copia2);
106+
}
107+
if(flag) continue;
108+
trinca sobra = ultimas(nova);
109+
int id1 = get_id(davez);
110+
int id2 = get_id(sobra);
111+
base.mat[id2][id1] = 1;
112+
}
113+
}
114+
115+
matrix resposta = expo(N - 3,base);
116+
ll tot = 0;
117+
for(int i = 0;i<MAXN;i++){
118+
for(int j = 0;j<MAXN;j++){
119+
tot = (tot + resposta.mat[i][j]) % MOD;
120+
}
121+
}
122+
123+
cout << tot << endl;
124+
125+
return 0;
126+
}

‎DMOJ/fibonacci2.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <bits/stdc++.h>
2+
#define REP(A,B) for(int (A) = 0;(A)<(B);(A)++)
3+
using namespace std;
4+
typedef long long ll;
5+
const int N = 2;
6+
const ll MOD = ll(1e9 + 7);
7+
const ll PISANO = 2000000016LL;
8+
struct matrix{
9+
ll mat[N][N];
10+
};
11+
matrix multiplication(matrix A,matrix B){
12+
matrix C;
13+
REP(i,N){
14+
REP(j,N){
15+
C.mat[i][j] = 0;
16+
}
17+
}
18+
REP(i,N){
19+
REP(j,N){
20+
REP(k,N){
21+
C.mat[i][j] += A.mat[i][k]*B.mat[k][j];
22+
}
23+
C.mat[i][j] %= MOD;
24+
}
25+
}
26+
return C;
27+
}
28+
matrix exponentation(ll pot,matrix base){
29+
matrix resp;
30+
matrix exponenciada = base;
31+
REP(i,N){
32+
REP(j,N){
33+
resp.mat[i][j] = (i == j);
34+
}
35+
}
36+
for(ll i = 0;(1LL << i) <= pot;i++){
37+
if((1LL << i) & pot) resp = multiplication(resp,exponenciada);
38+
exponenciada = multiplication(exponenciada,exponenciada);
39+
}
40+
return resp;
41+
}
42+
int main(){
43+
ll a = 0,b = 1;
44+
string pown;
45+
cin >> pown;
46+
reverse(pown.begin(),pown.end());
47+
ll somatorio = 0, pot = 1;
48+
for(char c : pown){
49+
ll digito = c - '0';
50+
somatorio += digito*pot;
51+
somatorio %= PISANO;
52+
pot *= 10;
53+
pot %= PISANO;
54+
}
55+
if(somatorio == 1){
56+
cout << b % MOD << endl;
57+
return 0;
58+
}
59+
if(somatorio == 0){
60+
cout << a % MOD << endl;
61+
return 0;
62+
}
63+
somatorio--;
64+
if(somatorio < 0) somatorio += PISANO;
65+
matrix base;
66+
base.mat[0][0] = 1;base.mat[0][1] = 1;
67+
base.mat[1][0] = 1;base.mat[1][1] = 0;
68+
matrix resp = exponentation(somatorio,base);
69+
ll exibe = resp.mat[0][0]*b + resp.mat[0][1]*a;
70+
exibe %= MOD;
71+
printf("%lld\n",exibe);
72+
return 0;
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /