## Sunday, April 10, 2016

### VK Cup Prep Series 4

 D

 D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed. To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a ≥ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k. What is the maximum possible score of the second soldier? Input First line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play. Then follow t lines, each contains pair of integers a and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value of n for a game. Output For each game output a maximum score that the second soldier can get. Examples Input ```2 3 1 6 3``` Output ```2 5``` ` ` `Idea :` `When you have more than 10^5 test cases and time in the range of 2-3 seconds You can be sure` ` Either you have to answer in log(N) But when you have 10^6 test cases It's better` `to pre-calculate answers and save them and the complexity would be preprocessing `  + testcase*O(1) . a!/b! = a*(a-1)*(a-2)*.....(b+1)

#)Using sieve to calculate any prime factor of a range of numbers.
#)Calculating all prime factors of a range of numbers in O(N).
#)Storing sum of all prime factors of a range of numbers using prefix sum.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133``` ```/**************************************** @_@ Cat Got Bored *_* #_# *****************************************/ #include #define loop(i,s,e) for(int i = s;i<=e;i++) //including end point #define pb(a) push_back(a) #define sqr(x) ((x)*(x)) #define CIN ios_base::sync_with_stdio(0); cin.tie(0); #define ll long long #define ull unsigned long long #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define ms(a,b) memset(a, b, sizeof(a)) #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define pf printf #define sfi(a) scanf("%d",&a); #define sfii(a,b) scanf("%d %d",&a,&b); #define sfl(a) scanf("%lld",&a); #define sfll(a,b) scanf("%lld %lld",&a,&b); #define mp make_pair #define paii pair #define padd pair #define pall pair #define fs first #define sc second #define CASE(t) printf("Case %d: ",++t) // t initialized 0 #define INF 1000000000 //10e9 #define EPS 1e-9 using namespace std; int primefactor[5000010]; //Some prime factor int num_ofPF[5000010]; // Number of prime factors ll cum_sum_num_PF[5000010]; // Cumulative sum of number of PF s in a range bool visited[5000010]; void sieve(int N) { int SqN = (int)sqrt(N+1); ms(visited,false); for(int i = 2 ; i<=N; i++) //Need to go till n otherwise I can set the prime factors for numbers > SqN { if(visited[i]==false) for(int j = i; j <= N ; j+=i) { visited[j] = true; primefactor[j] = i; } } } void num_of_prime_factors(int N) { num_ofPF[1] = 0; for(int i = 2; i<=N; i++) { // cout<

### VK Cup Prep Series 3

 C

 C. Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game. The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins. You have to calculate how many fights will happen and who will win the game, or state that game won't end. Input First line contains a single integer n (2 ≤ n ≤ 10), the number of cards. Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack. Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack. All card values are different. Output If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won. If the game won't end and will continue forever output  - 1. Examples Input ```4 2 1 3 2 4 2``` Output `6 2` Input ```3 1 2 2 1 3``` Output `-1` Note First sample: Second sample:                  Idea : Simulation + Max possible states calculation (Cycle detection)
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154``` ```/**************************************** @_@ Cat Got Bored *_* #_# *****************************************/ #include #define loop(i,s,e) for(int i = s;i<=e;i++) //including end point #define pb(a) push_back(a) #define sqr(x) ((x)*(x)) #define CIN ios_base::sync_with_stdio(0); cin.tie(0); #define ll long long #define ull unsigned long long #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define ms(a,b) memset(a, b, sizeof(a)) #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define pf printf #define sfi(a) scanf("%d",&a); #define sfii(a,b) scanf("%d %d",&a,&b); #define sfl(a) scanf("%lld",&a); #define sfll(a,b) scanf("%lld %lld",&a,&b); #define mp make_pair #define paii pair #define padd pair #define pall pair #define fs first #define sc second #define CASE(t) printf("Case %d: ",++t) // t initialized 0 #define INF 1000000000 //10e9 #define EPS 1e-9 using namespace std; queuefirstPlayer; queuesecondPlayer; ull allPossibleStatesCount() { /* Firstly let's count a number of different states that we can have in the game. Cards can be arranged in any one of n! ways. In every of this combination, we must separate first soldier's cards from the second one's. We can separate it in n+1 places (because we can count the before and after deck case too). */ ull states = 1; for(int i=1;i<=11;i++) states*=i; return states; } int main() { //Just using simulation int mxStates = allPossibleStatesCount(); int n,k1,k2; cin>>n>>k1; loop(i,1,k1) { int card; cin>>card; firstPlayer.push(card); } cin>>k2; loop(i,1,k2) { int card2; cin>>card2; secondPlayer.push(card2); } int simulationOn = 1; int statesCounter = 0; int winner = 0; while(simulationOn) { if(firstPlayer.empty()) { winner = 2; break; } if(secondPlayer.empty()) { winner = 1; break; } int fsCard = firstPlayer.front(); int scCard = secondPlayer.front(); firstPlayer.pop(); secondPlayer.pop(); if(fsCard>scCard) { firstPlayer.push(scCard); firstPlayer.push(fsCard); } if(fsCardmxStates) { winner = -1; break; } } if(winner==-1) cout<

### VK Cup Prep Series 2

 B

 B. Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness. Input First line of input consists of one integer n (1 ≤ n ≤ 3000). Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge. Output Output single integer — minimum amount of coins the colonel has to pay. Examples Input ```4 1 3 1 4``` Output `1` Input ```5 1 2 3 2 5``` Output `2` Note In first sample test we can increase factor of first badge by 1. In second sample test we can increase factors of the second and the third badge by 1. Idea : As you can only increase values by 1 ,  First Sorting and then increasing numbers 1 by 1 so that no duplicates are there will do it. Code :
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105``` ```/**************************************** @_@ Cat Got Bored *_* #_# *****************************************/ #include #define loop(i,s,e) for(int i = s;i<=e;i++) //including end point #define pb(a) push_back(a) #define sqr(x) ((x)*(x)) #define CIN ios_base::sync_with_stdio(0); cin.tie(0); #define ll long long #define ull unsigned long long #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define ms(a,b) memset(a, b, sizeof(a)) #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define pf printf #define sfi(a) scanf("%d",&a); #define sfii(a,b) scanf("%d %d",&a,&b); #define sfl(a) scanf("%lld",&a); #define sfll(a,b) scanf("%lld %lld",&a,&b); #define mp make_pair #define paii pair #define padd pair #define pall pair #define fs first #define sc second #define CASE(t) printf("Case %d: ",++t) // t initialized 0 #define INF 1000000000 //10e9 #define EPS 1e-9 using namespace std; int arr[3009]; int main() { int n; cin>>n; int ans = 0; loop(i,0,n-1) { cin>>arr[i]; } sort(arr,arr+n); /* loop(j,0,n-1) cout<

### VK Cup Prep Series 1

Stonefeang
#304 Div-2
 A

 A. Soldier and Bananas time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana). He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas? Input The first line contains three positive integers k, n, w (1  ≤  k, w  ≤  1000, 0 ≤ n ≤ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants. Output Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0. Examples Input `3 17 4` Output `13`  Code :

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95``` ```/**************************************** @_@ Cat Got Bored *_* #_# *****************************************/ #include #define loop(i,s,e) for(int i = s;i<=e;i++) //including end point #define pb(a) push_back(a) #define sqr(x) ((x)*(x)) #define CIN ios_base::sync_with_stdio(0); cin.tie(0); #define ll long long #define ull unsigned long long #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define ms(a,b) memset(a, b, sizeof(a)) #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define pf printf #define sfi(a) scanf("%d",&a); #define sfii(a,b) scanf("%d %d",&a,&b); #define sfl(a) scanf("%lld",&a); #define sfll(a,b) scanf("%lld %lld",&a,&b); #define mp make_pair #define paii pair #define padd pair #define pall pair #define fs first #define sc second #define CASE(t) printf("Case %d: ",++t) // t initialized 0 #define INF 1000000000 //10e9 #define EPS 1e-9 using namespace std; int main() { ull k,n,w; cin>>k>>n>>w; ull amount; ull w_1 = w+1; if(w%2==0) { w/=2; } else { (w_1)/=2; } amount = w*w_1*k; if(amount<=n) { cout<<0<

## Saturday, January 30, 2016

### T 1 ) 2 A Winner

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86``` ```#include using namespace std; struct round{ string name; int score; }; mapNameToScore; vectorAllRounds; setPotential; int main() { //Logic : See, who has the maximum points in the final round He is the winner //If there are multiple such contestant then the winner -> who had a better //or equal to that score in the most previous round int N; cin>>N; //How to simulate each round after I have taken all input -> save it for(int a = 0;a>s>>scr; struct round dummyRound; dummyRound.name = s; dummyRound.score = scr; AllRounds.push_back(dummyRound); NameToScore[s]+=scr; } int mx = -10000000; for(map::iterator it = NameToScore.begin();it!=NameToScore.end();it++) { if( it->second > mx ) { mx = it->second; } } for(map::iterator it = NameToScore.begin();it!=NameToScore.end();it++) { if( it->second == mx ) //All potential contestant { Potential.insert(it->first); } } //All rounds check NameToScore.clear(); for(int a = 0;a::iterator setIt; setIt = Potential.find(nm); if(NameToScore[nm] >= mx && setIt!=Potential.end()) { cout<