Posts

Showing posts with the label LightOj

LightOJ-1336 Sigma Function

 /* Look at this problem, here, N <= 10^12, which means this is impossible to take all values on an array. Therefore, it should be a line solution or simple mathematical solution. However, you have to analyze it with pen and paper first. I am giving a concept here: Look, if we have N, then, we may cover all prime or composite numbers on the range of sqrt(N). Besides, we have to find the result of even summation. So, if we have N, then, it must have N/2 even and odd numbers. We also cover those numbers under the range of sqrt(N/2). */ #include <bits/stdc++.h> using namespace std; #define sf scanf #define pf printf typedef long long LL; const int high = 1e6+5; void divisor_sum(int N) {     int i,j, x;     for(x=1; x<=N; x++)     {         LL sum = 0;         for(i=1; i*i<=x; i++)         {             if(x%i==0)           ...

LightOJ - Shadow Sum

 Problem Link  Shadow Sum /*     Accepted     It is a data structure problem. I have used the array and map to track same positive     , negative, single, and multiple times appeared numbers. Done the summation finally. */ #include <bits/stdc++.h> using namespace std; #define sf scanf #define pf printf typedef long long LL; const LL high = 2e4 + 10; int ar[high]; map<int,int>br; map<int,int>flag; void clean() {     br.clear();     flag.clear(); } int main() {     int i, N, test, tc=0;     sf("%d", &test);     while(test--)     {         clean();         sf("%d", &N);         for(i=0;i<N;i++)         {             sf("%d", &ar[i]);         }         //for(i=0;i<N;i++) cout << br[ar[i]] << "; "; ...

LightOJ 1245 - Harmonic Number (II)

Analysis Part: 1245 - Harmonic Number (II) Analysis Part /**   *  @Author: Pranta Sarker   *   **/ #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define pfn(x , k) printf(k , x) #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back #define clr clear() typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; typedef map<LL, LL>mpll; con...

Lightoj 1002 - Country Roads

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back #define clr clear() typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; typedef map<LL, LL>mpll; const int mod = 1000007; const int high = 503; const int inf = 2147483647; struct data {     int node, cost;     data() { }   ...

Lightoj 1019 - Brush (V)

// Accepted // Algorithm: Dijkstra #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; typedef map<LL, LL>mpll; const int mod = 1000007; const int high = 102; const int inf = 1e6+8; int dis[high], visited[high]; struct data {     int n...

LightOJ 1257 - Farthest Nodes in a Tree (II)

//The Solution of this problem simply easy than Lightoj 1094. // It is just 3 DFS problem. // To avoid TLE, you have to just take two maximum node with cost which started by node 0 // you have got first maximum node from node 0 and second maximum node from the 1st maximum node. :P // similarly you have to take distance(cost) for 1st maximum node and 2nd maximum node // Finally, just print the maximum cost between dist1[node] and dist2[node] /* Example: for the second test case:     0 2 20     2 1 10     0 3 29     0 4 50     from 0:           | 0-> 0           | 1-> 30     -----> 1st maximum node = 4           | 2-> 20           | 3-> 29           | 4-> ...

LightOj 1357 - Corrupted Friendship

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; typedef map<LL, LL>mpll; const int mod = 1000007; const int high = 100002; vii adj[high]; int visited[high]; LL nodeVisited=0 , notFriend=0; int N; void CLR() {     for(int i=0; i<...

Lightoj 1175 - Jane and the Frost Giants

// The most important line: Jane can escape from the maze from any squares that borders the edge of the maze. // There can be infinite number of 'J' or 'F' or both, but not exactly one. #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, ...

Lightoj 1049 - One Way Roads

/* The graph is a ring. Consider the third test case: 6 1 5 4 5 3 8 2 4 15 1 6 16 2 3 23 4 6 42 If we make a graph from these data, it will be look like the following: 1 --> 5 --> 3 <-- 2 --> 4 --> 6 <-- 1 and 2, 4 connected together. You can easily decompose the edges in the two different directions: 1. Edges which goes to the left: 3 <-- 2     23 6 <-- 1        16 Total cost: 39 2. Edges which goes to the right: 1 --> 5         4 5 --> 3         8 2 --> 4        15 4 --> 6        42 Total cost: 69 Remember, 'left' or 'right' is not important here, you can easily build a graph with left and right altered. All we care is the two different direction of the edges. Choose the direction in which cost is minimum (here, left, cost is 39). And build roads in the opposite direction. So in this ca...

Lightoj 1263 - Equalizing Money

#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; typedef map<LL, LL>mpll; const int mod = 1000007; const int high = 1002; int money[high] , visited[high] , person=0; LL sum = 0; vii adj[high]; void DFS(int u) {     visited[u] = 1;  ...

Lightoj 1141 - Number Transformation

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int...

Ligthoj 1238 - Power Puff Girls

===================== Best Solution =========================== /* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted by One BFS #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<in...

1012 - Guilty Prince

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Algorithm: BFS (2D) // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typ...

Lightoj 1094 - Farthest Nodes in a Tree

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Algorithm: DFS // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef ...

Lightoj 1174 - Commandos

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ /* Run BFS from the Source and Destination besides store the distance for all nodes on two different array when you run BFS from Source and Destination. Pick the biggest time with Summation from your stored distances... :) You have to find the minimum time that's why you have to Visit from both Source and Destination and at last you have to pick the biggest time for the minimum ... */ // Algorithm: BFS // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i...