Abridged problem statement To N cards, each of the front and back are red and blue, each with a number, you can choose two cards at a time, you can choose a card with a blue face number and the other card red face The result is added to your score (initial score = 0), then discard a card and put back a card until you are left with just 1 card Constraints 1 ≤ T ≤ 100. 1 ≤ R i ≤ 10 9 . 1 ≤ B i ≤ 10 9 . Small dataset 2 ≤ N ≤ 5. Large dataset 2 ≤ N ≤ 100. Approach On careful observation we observethat the minimum requirement for the sum of the weights and bounds is actually the minimum spanning tree formed by n points. Kruskal can be used to construct the MST Code # include < iostream > # include < vector > # include < set > # include < utility > # include < tuple > # include < algorithm > using namespace std ; int red [ 105 ] , blue [ 105 ] ; int union_find [ 105 ] ; int find ( int x ) ...
Abridged problem statement Compute A N! % P Constraints 1 ≤ T ≤ 100. Small dataset 1 ≤ A ≤ 10. 1 ≤ N ≤ 10. 1 ≤ P ≤ 10. Large dataset 1 ≤ A ≤ 10 5 . 1 ≤ N ≤ 10 5 . 1 ≤ P ≤ 10 5 . Approach Just use the fact that A m*n = (A m ) n A m*n % P = ( (A m % P) n % P ) Code # include < iostream > using namespace std ; int power ( long long x , int n , int M ) { long long result = 1 ; while ( n > 0 ) { if ( n % 2 = = 1 ) result = ( result * x ) % M ; x = ( x * x ) % M ; n = n / 2 ; } return result ; } int main ( ) { freopen ( " A-large-practice.in " , " r " , stdin ) ; freopen ( " output-large.txt " , " w " , stdout ) ; int t , a , n , p ; long long ans ; cin > > t ; for ( int i = 1 ; i < = t ; i + + ) { cin > > a > > n > > p ; ans = a ; fo...