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 b99d771

Browse files
Repository has been unarchived
1 parent 0dc79e6 commit b99d771

File tree

4 files changed

+322
-0
lines changed

4 files changed

+322
-0
lines changed

‎SPOJ/COMDIV.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Ivan Carvalho
2+
// Solution to https://www.spoj.com/problems/COMDIV/
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
const int MAXN = 1e6 + 10;
7+
8+
int divisors[MAXN];
9+
10+
int gcd(int x,int y){
11+
if(x < y) swap(x,y);
12+
if(y == 0) return x;
13+
return gcd(y,x%y);
14+
}
15+
16+
int main(){
17+
18+
for(int i = 1;i<MAXN;i++){
19+
for(int j = i;j<MAXN;j+=i) divisors[j]++;
20+
}
21+
22+
int TC;
23+
scanf("%d",&TC);
24+
25+
while(TC--){
26+
int a,b;
27+
scanf("%d %d",&a,&b);
28+
printf("%d\n",divisors[gcd(a,b)]);
29+
}
30+
31+
return 0;
32+
}

‎SPOJ/FASTFLOW.cpp

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
Tidal Flow Algorithm by M. Fontaine :
3+
https://ioinformatics.org/journal/v12_2018_25_41.pdf
4+
Implementation by I. Carvalho
5+
Solution to FASTFLOW : https://www.spoj.com/problems/FASTFLOW/
6+
Note : In this problem, node 1 is the source and node N is the Sink. The
7+
edges are also bidirectional
8+
*/
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
typedef long long ll;
13+
14+
const int MAXN = 5010;
15+
const int MAXM = 30010;
16+
const ll INF = 1e18;
17+
18+
struct edge{
19+
20+
int to,rev; // node, index in the other adjacency list
21+
ll cap; // residual capacity
22+
23+
};
24+
25+
vector<edge> adjList[MAXN]; // adjacency list of the edges
26+
int Ev[MAXM],Ei[MAXM]; // BFS edge list
27+
int N,M,bfsPtr; // number of nodes
28+
int dist[MAXN],ok[MAXN];
29+
30+
ll h[MAXN]; // heuristic of Tidal Flow
31+
ll p[MAXM]; // promised flow of Tidal Flow
32+
ll l[MAXN]; // amount of flow in pool of Tidal FLow
33+
34+
ll TidalFlow(int edgeListSize){
35+
36+
int source = 1, sink = N;
37+
38+
fill(h,h+N+1,0); // initializing h[v], for all v
39+
h[source] = INF; // there is no bound in the flow that can reach
40+
the source
41+
42+
for(int i = 0;i<edgeListSize;i++){
43+
int w = Ev[i]; // where it comes from
44+
int idx = Ei[i]; // index in the adjList
45+
int v = adjList[w][idx].to; // where it goes
46+
p[i] = min(adjList[w][idx].cap,h[w]); // the promised is
47+
the minimum of the heuristic and the capacity
48+
h[v] += p[i]; // we add the promised capacity to the
49+
heuristic
50+
}
51+
52+
if(h[sink] == 0) return 0; // if the heuristic for the sink is
53+
0, then we are done
54+
55+
fill(l,l+N+1,0); // initializing l[v], for all v
56+
l[sink] = h[sink]; // the amount of flow is the heuristic in the
57+
sink, initially
58+
59+
for(int i = edgeListSize - 1;i>=0;i--){
60+
int w = Ev[i]; // where it comes from
61+
int idx = Ei[i]; // index in the adjList
62+
int v = adjList[w][idx].to; // where it goes
63+
// the promised is the minimum of the promised before,
64+
the diference between heuristic and available of the start or the
65+
available of the end
66+
p[i] = min(p[i], min(h[w] - l[w],l[v]));
67+
l[v] -= p[i];
68+
l[w] += p[i];
69+
}
70+
71+
fill(h,h+N+1,0); // h[v] = 0, for all v
72+
h[source] = l[source]; // the amount of pool is the heuristic
73+
for(int i = 0;i<edgeListSize;i++){
74+
int w = Ev[i]; // where it comes from
75+
int idx = Ei[i]; // index in the adjList
76+
int v = adjList[w][idx].to; // where it goes
77+
int rev_idx = adjList[w][idx].rev; // index in the other
78+
adjList
79+
p[i] = min(p[i],h[w]);
80+
h[w] -= p[i];
81+
h[v] += p[i];
82+
adjList[w][idx].cap -= p[i]; // we update the residual
83+
capacity
84+
adjList[v][rev_idx].cap += p[i]; // update reverse edje
85+
capacity
86+
}
87+
88+
return h[sink];
89+
90+
}
91+
92+
int BFS(){
93+
94+
bfsPtr = 0;
95+
fill(ok,ok+N+1,0);
96+
97+
int source = 1,sink = N;
98+
99+
queue<int> bfs;
100+
bfs.push(source);
101+
ok[source] = 1;
102+
dist[source] = 0;
103+
104+
while(!bfs.empty()){
105+
106+
int v = bfs.front();
107+
bfs.pop();
108+
109+
for(int i = 0;i<adjList[v].size();i++){
110+
if(adjList[v][i].cap == 0) continue;
111+
int u = adjList[v][i].to;
112+
if(ok[u]){
113+
if(dist[u] == dist[v] + 1){
114+
Ev[bfsPtr] = v;
115+
Ei[bfsPtr] = i;
116+
bfsPtr++;
117+
}
118+
continue;
119+
}
120+
ok[u] = 1;
121+
dist[u] = dist[v] + 1;
122+
bfs.push(u);
123+
Ev[bfsPtr] = v;
124+
Ei[bfsPtr] = i;
125+
bfsPtr++;
126+
}
127+
128+
}
129+
130+
return bfsPtr;
131+
132+
}
133+
134+
ll FordFulkerson(){
135+
136+
ll flow = 0;
137+
138+
while(true){
139+
ll augmenting = TidalFlow(BFS());
140+
if(augmenting == 0) break;
141+
else flow += augmenting;
142+
}
143+
144+
return flow;
145+
146+
}
147+
148+
void addEdge(int u,int v,int cap){
149+
150+
edge newedge;
151+
int uPtr = (int)adjList[u].size();
152+
int vPtr = (int)adjList[v].size();
153+
154+
newedge.to = v;
155+
newedge.rev = vPtr;
156+
newedge.cap = cap;
157+
adjList[u].push_back(newedge);
158+
159+
newedge.to = u;
160+
newedge.rev = uPtr;
161+
newedge.cap = cap;
162+
adjList[v].push_back(newedge);
163+
164+
}
165+
166+
int main(){
167+
168+
scanf("%d %d",&N,&M);
169+
170+
for(int i = 1;i<=M;i++){
171+
int u,v,w;
172+
scanf("%d %d %d",&u,&v,&w);
173+
addEdge(u,v,w);
174+
}
175+
176+
printf("%lld\n",FordFulkerson());
177+
178+
return 0;
179+
180+
}

‎SPOJ/GCDEX.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Ivan Carvalho
2+
// Solution to https://www.spoj.com/problems/GCDEX/
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
8+
const int MAXN = 1e6 + 10;
9+
10+
ll G[MAXN];
11+
int isNotPrime[MAXN],phi[MAXN];
12+
13+
int main(){
14+
15+
// Initializing
16+
for(int i = 1;i<MAXN;i++){
17+
phi[i] = i;
18+
}
19+
20+
// Calculating phi
21+
for(int i = 2;i<MAXN;i++){
22+
if(isNotPrime[i]) continue;
23+
phi[i]--;
24+
for(int j = 2*i;j<MAXN;j+=i){
25+
phi[j] -= phi[j]/i;
26+
isNotPrime[j] = 1;
27+
}
28+
}
29+
30+
// Calculating G
31+
for(int i = 1;i<MAXN;i++){
32+
for(int j = 2*i;j<MAXN;j+=i){
33+
G[j] += 1LL*i*phi[j/i];
34+
}
35+
}
36+
37+
for(int i = 2;i<MAXN;i++) G[i] += G[i-1]; // pref sum
38+
39+
int N;
40+
while(scanf("%d",&N) && N){
41+
printf("%lld\n",G[N]);
42+
}
43+
44+
return 0;
45+
46+
}

‎SPOJ/LCMSUM.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Ivan Carvalho
2+
// Solution to https://www.spoj.com/problems/LCMSUM/
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
8+
const int MAXN = 1000010;
9+
10+
ll lcmSum[MAXN],sumOfCoprimes[MAXN];
11+
ll mobius[MAXN],isNotPrime[MAXN];
12+
13+
ll fast_sum(ll x){
14+
return (x*(x+1))/2;
15+
}
16+
17+
int main(){
18+
19+
//cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
20+
21+
// Initializing values
22+
for(int i = 1;i<MAXN;i++){
23+
mobius[i] = 1;
24+
}
25+
26+
// Calcuting primes and mobius function
27+
for(int i = 2;i<MAXN;i++){
28+
if(isNotPrime[i]) continue;
29+
mobius[i] *= -1;
30+
for(int j = 2*i;j<MAXN;j+=i){
31+
mobius[j] *= -1;
32+
isNotPrime[j] = 1;
33+
}
34+
ll sq = 1LL*i*i;
35+
for(ll j = sq;j<MAXN;j+=sq) mobius[j] = 0;
36+
}
37+
38+
// Calculating the sum of coprimes
39+
for(int i = 1;i<MAXN;i++){
40+
if(mobius[i] == 0) continue;
41+
for(int j = i;j<MAXN;j+=i){
42+
sumOfCoprimes[j] +=
43+
1LL*fast_sum(j/i)*i*mobius[i];
44+
}
45+
}
46+
47+
// Calculating the LCM sum
48+
for(ll i = 1;i<MAXN;i++){
49+
for(ll j = i;j<MAXN;j+=i){
50+
lcmSum[j] += j*sumOfCoprimes[j/i];
51+
}
52+
}
53+
54+
int TC;
55+
scanf("%d",&TC);
56+
for(int i = 1;i<=TC;i++){
57+
int N;
58+
scanf("%d",&N);
59+
printf("%lld\n",lcmSum[N]);
60+
}
61+
62+
return 0;
63+
64+
}

0 commit comments

Comments
(0)

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