Posts

Showing posts from January, 2019

UVa 10806 - Dijkstra, Dijkstra.

/*     Accepted     Handle conditions properly and try to make optimize     Dijkstra to avoid TLE     At first, I have run Dijkstra to find the minimum time is taken by first friend.     Then, mark parent and child so that second friend can't visit those nodes.     And finally, again run Dijkstra to find minimum time for the second friend if possible.     Answer should be summation of both times. */ #include<bits/stdc++.h> using namespace std; #define sf scanf #define pf printf #define fast ios_base::sync_with_stdio(false) typedef long long LL; const int high = 110; const int inf = (1<<31)-1; struct data {     int node, cost;     data(){ }     data(int n, int c)     {         node = n;         cost = c;     }     bool operator<(const data &b)const     {         return cost>b.cost;     } }; vector<data>adj[high]; int visited[high][high], par[high], min_cost[high]; void CLR(int n) {     for(int i=1;i<=n;i++)     {         adj[i].clear();         for(in

UVa 11718 - Fantasy of a Summation

 Link: https://uva.onlinejudge.org/external/117/11718.pdf /*     Accepted     sum = ( A[0] + A[1] + A[2] + ... + A[k] ) % M     ans = (sum * N ^ (K-1) * K) % M                  ---------                  Big Mod     [N:B: Use Big Mod to avoid TLE] */ #include<bits/stdc++.h> using namespace std; #define sf scanf #define pf printf #define psb push_back #define fast ios_base::sync_with_stdio(false) typedef map<string, int>mpsi; typedef map<int, string>mpis; typedef long long LL; const int high = 1005; const int inf = (1 << 31) - 1; LL get_power(int N, int K, int M) {     LL x = 1;     if(K == 0) return 1;     x = get_power(N , K/2, M);     if(K%2==0)     {         return x = ((x % M) * (x % M)) % M;     }     else     {         return x = ((N % M) * ((x % M) * (x % M))) % M;     } } int main() {     fast;     int i, test, tc=0 , x;     int N, K , M;     cin >> test;     while(test--)     {         cin >> N >> K >> M;         LL sum = 0;