hello i'm trying to apply fft on this problem
i'm tring to optimize O(n^2) operation that found every combination of x[i] and y[j] (0 <= i,j <= N) by fft
but after spending short time on studing fft
but since it is not a multiplication operation,
i wonder whether i can apply fft on these kind of problem
would you give me any way to solve this problem?
i'm sorry for my poor english.
below is wrong answer by me,
it spent O(N^2)
, just help for understand problem
#include <iostream>
#include <cmath>
#define MAX 262145
using namespace std;
typedef long long LL;
int N;
LL ans[MAX] = {0,};
LL c[19][2][2] = {0,}; // c [2^i][xi][yi]
int x[] = {0,0,1,1};
int y[] = {0,1,0,1};
LL A[MAX] = {0,}; //Set A
LL B[MAX] = {0,}; //Set B
LL power[18] = {0,}; //power[i] == pow(2,i)
LL cal(LL a,LL b){ // find the index number for input pair(a,b)
LL res = 0;
for(int i = 0 ; i < N ; i++){
res += c[i][(a&(1<<i)) == 0 ? 0 : 1][(b&(1<<i)) == 0 ? 0 : 1] * power[i];
}
return res; // the result will point the index of f(x,y)
}
int main() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
cin >> N;
string str;
int mv = pow(2,N);
for(int i = 0 ; i < N ; i++){
cin >> str;
power[i] = pow(2,i);
for(LL j = 0 ; j < 4 ; j++){
c[i][x[j]][y[j]] = (int)(str[j] - '0');
}
}
for(int i = 0 ; i < mv ; i++){
cin >> A[i];
}
for(int i = 0 ; i < mv ; i++){
cin >> B[i];
if(B[i] != 0){
for(int j = 0 ; j < mv ; j++){
if(A[j] != 0){
ans[cal(j,i)] += A[j]*B[i]; //
}
}
}
} // in this loop we use O(N^2)
for(int i = 0 ; i < mv ; i++){
cout << ans[i] << " ";
}
return 0;
}
problem:
enter image description here
Related
https://cses.fi/problemset/task/1649
I'm solving this problem using Segment Trees and the solution I've written is
#include <bits/stdc++.h>
#define MAX 1000000001
using namespace std;
int n;
vector<int> tree;
int sum(int a, int b)
{
a += n;
b += n;
int s = INT_MAX;
while(a <= b) {
if (a % 2 == 1) s = min(s, tree[a++]);
if (b % 2 == 0) s = min(s, tree[b--]);
a>>=1;
b>>=1;
}
return s;
}
void update(int k, int change)
{
k += n;
tree[k] = change;
for(int i = k>>1; i >= 1; i>>=1) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
return;
}
int main()
{
int q;
cin >> n >> q;
n = pow(2, ceil(log2(n)));
tree.resize(2*n, INT_MAX);
for(int i = 0; i < n; i++) {
cin >> tree[i+n];
}
for(int i = n-1; i >= 1; i--) {
tree[i] = min(tree[2*i], tree[2*i+1]);
}
int type, a, b;
for(int i = 0; i < q; i++) {
cin >> type >> a >> b;
if (type == 1) {
update(a-1, b);
} else {
cout << sum(a-1, b-1) << endl;
}
}
return 0;
}
It works with first test case, but not with the second one. I've looked at other solutions online and they all look similar to mine. Please, help me spot the mistake.
So we are given a permutation of the numers {1... N}.
We are given an integer k and then k queries of this type:
q(x,y,l,r) - count numbers between position X and Y in the permutation, which are >=l and <=r.
For example:
N - 7: (1 6 3 5 7 4 2)
q(1,4,2,7) -> 3 numbers ( 6, 3 and 5 , since 2<=6<=7 , 2<=3<=7 and 2<=5<=7)
So my attempt was to store the permutation and and position array (too have fast acces to the position of each number)
Then i check which interval is smaller [x,y] or [l,r] and iterate through the smaller.
The answers i get are correct, but i get 0 points, since my solution it's too slow.
Any tips how to make this queries as fast as possible for big N?
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int q;
cin >> q;
int* perm = new int[n+1];
int* pos = new int[n+1];
for (int i = 1; i <= n; i++)
{
int num;
cin >> num;
perm[i] = num;
pos[num] = i;
}
for (int i = 0; i < q; i++)
{
int x, y, l, r;
cin >> x >>y>> l>> r;
int count = 0;
if (y - x < r - l)
{
for (int i = x; i <= y; i++)
{
if (perm[i] >= l && perm[i] <= r)
count++;
}
cout << count << endl;
}
else
{
int count = 0;
for (int i = l; i <= r; i++)
{
if (pos[i] >= x && pos[i] <= y)
count++;
}
cout << count << endl;
}
}
}
Here's the link for the problem:
I have used union-find algorithm to solve the problem.
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define INT 100000000
unordered_map<ll, ll> parent;
unordered_map<ll, ll> depth;
std::vector<ll> cost;
ll find_set(ll x) {
if (x == parent[x])return x;
parent[x] = find_set(parent[x]);
return parent[x];
}
void union_set(ll x, ll y) {
/*
Creating a disjoint set such that the node with smallest cost
being the root using union-rank concept.
*/
ll rep1 = find_set(x), rep2 = find_set(y);
if (depth[rep1] > depth[rep2])parent[rep1] = rep2;
else if (depth[rep2] >= depth[rep1])parent[rep2] = rep1;
}
int main() {
ll n, m;
cin >> n >> m;
ll c[m + 1][3];
for (ll i = 1; i <= m; i++) {
cin >> c[i][1] >> c[i][2]; //Accepting the edges
}
for (ll i = 1; i <= n; i++) {
parent[i] = i;
cin >> depth[i];
if (depth[i] < 0)depth[i] = INT;
/*we assume that each negative cost is replaced by a very
large positive cost.*/
}
for (ll i = 1; i <= m; i++) {
union_set(c[i][1], c[i][2]);
}
set<ll> s;
std::vector<ll> v;
//storing representatives of each connected component
for (auto i = 1; i <= n; i++)s.insert(depth[find_set(i)]);
for (auto it = s.begin(); it != s.end(); it++)v.push_back(*it);
sort(v.begin(), v.end());
if (s.size() == 1) {
//Graph is connected if there is only 1 connected comp
cout << 0 << endl;
return 0;
}
bool flag = false;
ll p = 0;
for (ll i = 1; i < v.size(); i++) {
if (v[i] == INT) {
flag = true;
break;
}
p += (v[0]+v[i]);
}
if (flag)cout << -1 << endl;
else cout << p << endl;
return 0;
}
Logic used in my program:
To find the answer, take the minimum value of all the valid values in a connected component.Now to make the graph connected, Take the minimum value of all the values we got from the above step and make edge from that node to all the remaining nodes.If graph is already connected than answer is 0.if there exists a connected component where all nodes are not valid to be chosen, than answer is not possible (-1).
But this solution is not accepted?What's wrong with it?
How can I calculate number of partitions of n mod 1e9+7, where n<=50000.
See http://oeis.org/A000041 .
Here is the source problem http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259 (In Chinese)
Simply applying the formula: a(n) = (1/n) * Sum_{k=0..n-1} d(n-k)*a(k) gave me an O(n^2) solution.
typedef long long ll;
ll MOD=1e9+7;
ll qp(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%MOD;
a=a*a%MOD;
b>>=1;
}
return ans;
}
ll a[50003],d[50003];
#define S 1000
int main()
{
for(int i=1; i<=S; i++)
{
for(int j=1; j<=S; j++)
{
if(i%j==0) d[i]+=j;
}
d[i]%=MOD;
}
a[0]=1;
for(int i=1; i<=S; i++)
{
ll qwq=0;
for(int j=0; j<i; j++) qwq=qwq+d[i-j]*a[j]%MOD;
qwq%=MOD;
a[i]=qwq*qp(i,MOD-2)%MOD;
}
int n;
cin>>n;
cout<<a[n]<<"\n";
}
I would solve it with a different approach.
Dynamic Programming:
DP[N,K] = number of partitions of N using only numbers 1..K
DP[0,k] = 1
DP[n,0] = 0
DP[n,k] when n<0 = 0
DP[n,k] when n>0 = DP[n-k,k] + DP[n,k-1]
Recursive implementation using memoization:
ll partition(ll n, ll max){
if (max == 0)
return 0;
if (n == 0)
return 1;
if (n < 0)
return 0;
if (memo[n][max] != 0)
return memo[n][max];
else
return (memo[n][max] = (partition(n, max-1) + partition(n-max,max)));
}
Live-Example
i am clueless as to what is wrong with this piece of code.
its not working as expected.
its expected to display the shortest path from vertex 1 to N.
but its failing on a lot of cases.
one such case is
3 1
1 2 1
it shows the answer as
1 25 -1 3
which is wrong...
any help would be appreciated.
Thanks.
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
using namespace std;
struct Edge{
int I, W;
};
vector <int> dist;
vector <int> parent;
bool bellman_ford(const vector< vector <Edge> > &graph, int n){
dist[1] = 0;
parent[1] = 0;
for(int k = 1; k <= n-1; k++){
for(int i = 1; i <= n; i++){
int len = graph[i].size();
for(int j = 0; j < len; j++){
int v = graph[i][j].I;
int w = graph[i][j].W;
if(dist[v] > dist[i] + w){
dist[v] = dist[i] + w;
parent[v] = i;
}
}
}
}
for(int i = 1; i <= n; i++){
int len = graph[i].size();
for(int j = 0; j < len; j++){
int v = graph[i][j].I;
int w = graph[i][j].W;
if(dist[v] > dist[i] + w){
return false;
}
}
}
return true;
}
int main(void){
int n, m, x, y, w;
scanf("%d%d", &n, &m);
dist.resize(n+1, 10000000);
parent.resize(n+1, -1);
vector < vector <Edge> > graph (n+1, vector <Edge> (0)) ;
for(int i = 0; i < m; i++){
scanf("%d%d%d", &x, &y, &w);
Edge a, b;
a.I = y;
b.I = x;
a.W = b.W = w;
graph[x].push_back(a);
graph[y].push_back(b);
}
if(bellman_ford(graph, n)){
int k = n;
vector<int>ans;
ans.push_back(n);
while(parent[k] != 0){
ans.push_back(parent[k]);
k = parent[k];
}
for(int i = ans.size()-1; i >= 0; i--){
printf("%d ", ans[i]);
}
printf("\n");
}
}
For the input case 3 1 1 2 1 you have a graph of 3 vertices, but the graph has only a single edge (1->2):
(1)<~~>(2) (3)
so the vertex numbered 3 (n) is never reached. The parent node of 3 is set to initial value -1, your loop searches for 0. You have no check if there actually is a path from a source to its target or not at all. The output is correct until -1:
3 - target
-1 - target has no parent, the loop should stop
25 - *garbage* (UB)
1 - *garbage* (UB)