Skip to content

Commit 6b159e4

Browse files
authored
Merge pull request ashutosh97#140 from rishabhdeepsingh/master
Added POLYMUL from SPOJ
2 parents 8b3c7e4 + 270e0ab commit 6b159e4

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

PolyMul/question.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Sam and Dean fight the supernatural creatures to protect the humans. Now they have come across a creature called the Vedala, which are always found in pairs. Each vedala can be represented by a polynomial. Both the Vedalas need to be killed at once or else they can't be killed. They can be killed only by using the product of the two polynomials representing the two Vedala. Help Sam and Dean find the product of the polynomials fast, so they can do thier work.
2+
3+
Input
4+
5+
First line contains an integer T (≤ 10), number of test cases.
6+
Each test case will have n (n ≤ 10000), maximum degree of the polynomials on the first line.
7+
8+
Next two lines will have n+1 space separated integers each representing the coeffiecients of first and second polynomials respectively.
9+
10+
All input coefficients values are <=1000.
11+
12+
Output
13+
For each test case ouput a line of 2n space seperated integers indicating coefficients of the polynomial created after multiplication.
14+
15+
16+
Example
17+
Input:
18+
2
19+
2
20+
1 2 3
21+
3 2 1
22+
2
23+
1 0 1
24+
2 1 0
25+
26+
Output:
27+
3 8 14 8 3
28+
2 1 2 1 0
29+
30+
31+

PolyMul/solution.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int LL;
6+
typedef vector<LL> VLL;
7+
typedef std::complex<double> CD;
8+
9+
#define PB push_back
10+
#define F first
11+
#define s second
12+
#define SZ(x) x.size()
13+
#define ALL(a) std::begin(a), std::end(a)
14+
#define IN_REP int _t; cin >> _t ; while(_t--)
15+
#define IOS ios::sync_with_stdio(false);cin.tie(NULL)
16+
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
17+
#define REP(i, n) FOR(i,0,n)
18+
const double PI = acos(-1);
19+
typedef std::valarray<CD> CArray;
20+
21+
// Cooley–Tukey FFT (in-place)
22+
void fft(CArray &x) {
23+
// const size_t N = x.size();
24+
int N = SZ(x);
25+
if (N <= 1) return;
26+
27+
// divide
28+
CArray even = x[std::slice(0, N / 2, 2)];
29+
CArray odd = x[std::slice(1, N / 2, 2)];
30+
31+
// conquer
32+
fft(even);
33+
fft(odd);
34+
35+
// combine
36+
// for (size_t k = 0; k < N/2; ++k){
37+
for (int k = 0; k < N / 2; ++k) {
38+
CD t = std::polar(1.0, -2 * PI * k / N) * odd[k];
39+
x[k] = even[k] + t;
40+
x[k + N / 2] = even[k] - t;
41+
}
42+
}
43+
44+
// inverse fft (in-place)
45+
void ifft(CArray &x) {
46+
// conjugate the complex numbers
47+
x = x.apply(std::conj);
48+
49+
// forward fft
50+
fft(x);
51+
52+
// conjugate the complex numbers again
53+
x = x.apply(std::conj);
54+
55+
// scale the numbers
56+
x /= x.size();
57+
}
58+
59+
int main() {
60+
IOS;
61+
IN_REP {
62+
int n;
63+
cin >> n;
64+
n++; // Given in question
65+
int size = 2 * (1 << int(ceil(log2(n))));
66+
67+
CArray x(size), y(size);
68+
FOR(i, size - n, size) cin >> x[i];
69+
fft(x);
70+
FOR(i, size - n, size) cin >> y[i];
71+
fft(y);
72+
73+
CArray res(size);
74+
res = x * y;
75+
ifft(res);
76+
VLL ans;
77+
REP(i, size - 1) {
78+
ans.PB(round(res[i].real()));
79+
}
80+
for (int i = size - 1 - (2 * n - 1); i < size - 1; i++) {
81+
cout << ans[i] << " ";
82+
}
83+
cout << endl;
84+
}
85+
return 0;
86+
}

Strange_Food_Chain/question.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
There are 3 kinds of animals A,B and C. A can eat B,B can eat C,C can eat A. It's interesting,isn't it?
2+
3+
Now we have n animals,numbered from 1 to n. Each of them is one of the 3 kinds of animals:A,B,C.
4+
5+
Today Mary tells us k pieces of information about these n animals. Each piece has one of the two forms below:
6+
7+
1 x y: It tells us the kind of x and y are the same.
8+
2 x y: It tells us x can eat y.
9+
Some of these k pieces are true,some are false. The piece is false if it satisfies one of the 3 conditions below, otherwise it's true.
10+
11+
X or Y in this piece is larger than n.
12+
This piece tells us X can eat X.
13+
This piece conflicts to some true piece before.
14+
Input
15+
The first line contains a single integer t.t blocks follow.
16+
17+
To every block,the first line contains two integers n(1<=n<=50000) and k (1<=k<=100000). k lines follow,each contains 3 positive integers D(1<=D<=2),X,Y,separated by single spaces.
18+
19+
20+
Output
21+
t lines,each contains a single integer - the number of false pieces in the corresponding block.
22+
23+
24+
Example
25+
Sample input:
26+
1
27+
100 7
28+
1 101 1
29+
2 1 2
30+
2 2 3
31+
2 3 3
32+
1 1 3
33+
2 3 1
34+
1 5 5
35+
36+
Sample output:
37+
3

Strange_Food_Chain/solution.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
const int N = 5e4 + 10;
7+
8+
int n, k;
9+
int par[N], lab[N];
10+
11+
int anc(int u) {
12+
if (u == par[u]) return u;
13+
int tmp = par[u];
14+
par[u] = anc(par[u]);
15+
lab[u] = lab[tmp] + lab[u];
16+
return par[u];
17+
}
18+
19+
int main() {
20+
// freopen("input.in", "r", stdin);
21+
// freopen("output.out", "w", stdout);
22+
23+
int test; scanf("%d", &test);
24+
while (test--) {
25+
scanf("%d%d", &n, &k);
26+
for (int i = 1; i <= n; ++i) {
27+
par[i] = i;
28+
lab[i] = 0;
29+
}
30+
int answer = 0;
31+
while (k--) {
32+
int t, x, y; scanf("%d%d%d", &t, &x, &y);
33+
if (x > n || y > n) { answer++; continue; }
34+
int px = anc(x), py = anc(y);
35+
t--;
36+
if (px == py) {
37+
int tmp = (lab[x] - lab[y]) % 3; if (tmp < 0) tmp += 3;
38+
if (tmp != t) answer++;
39+
} else {
40+
par[px] = py;
41+
int i = (lab[x] - lab[y] - t) % 3;
42+
lab[px] = i < 0 ? -i : -i + 3;
43+
}
44+
}
45+
printf("%d\n", answer);
46+
}
47+
48+
return 0;
49+
}
50+
51+

0 commit comments

Comments
 (0)