Posts

URI/BEE 3165/beecrowd | 3165 - Twin Prime

/* You have to find out two nearest prime numbers of N which have the maximum size less than or equals to N and the gap between them is two. In this code/algorithm, since the input data is subtle by 10^3, in that case, any naive formulation to find prime numbers is acceptable. Thus, we have applied Sieve to make separate all composite and prime numbers and then the Twin_Prime() function figure out the two adjacent prime numbers in such as way that each number is updated by its adjacent prime number which has a difference is two. */ #include<stdio.h> #define high 1007 #define sf scanf #define pf printf int primes[high], flag[high]; void init() {     int i=0;     for(; i<high; i++)     {         flag[i] = 0;     } } void Sieve() {     int i=0, j=0;     for(i=2; i<high; i++)     {         if(flag[i] == 0)         {           ...

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]] << "; "; ...

ARC116 - A - Odd vs Even

 /*     There is a pattern to solve this problem:     1 -> odd     2 -> same     3 -> odd     4 -> even     5 -> odd     6 -> same     7 -> odd     8 -> even     9 -> odd     10 -> same     11 -> odd     12 -> even     13 -> odd     14 -> same     15 -> odd     16 -> even     17 -> odd     18 -> same     19 -> odd     20 -> even     Observation:     1. same number of even and odd divisors are repeating after 4 steps     2. we are getting more even number of divisors which is divided by 4     3. a typical odd number is generating much number of odd divisors     Now, let's implement those observations... */ #include <bits/stdc++.h> using namespace std; #define sf ...

Atcode ABC - A - Rotate

 #include<bits/stdc++.h> using namespace std; #define sf scanf #define pf printf const int high=1e3+5; int main() {     string s, ans="";     while(cin >> s)     {         int len = s.length();         for(int i=1; i<len; i++)         {             ans += s[i];         }         ans+=s[0];         cout << ans << "\n";         ans.clear();     }     return 0; }

Atcoder ABC 197 - B - Visibility

/*     Count '.' - from the same row and column and count each     Finally, subtract 3, as (x, y) repeated like 3 times */ #include<bits/stdc++.h> using namespace std; #define sf scanf #define pf printf const int high=1000+5; char adj[high][high]; int main() {     int i, j, h, w, x, y;     cin >> h >> w >> x >> y;     for(i=0; i<h; i++)     {         for(j=0; j<w; j++)         {             cin >> adj[i][j];         }     }     int cnt=0;     x--; y--;     for(i=x; i<h; i++) //row - down     {         if(adj[i][y]=='#') break;         cnt+=1;     }     for(i=x; i>=0; i--) // row - up     {         if(adj[i][y]=='#') break;         cnt+...

Codeforces #710 - Strange Table

 /*     Solving concept:     I have first found the position of x in "by columns":         This is note that the position is started from 1. So,         if(x % n == 0) then, row = n, col = x / n;         else row = x % n, col = (x / n) + 1;         This is an easy technique.         Mind that: you can not run any loop to find the position because of the large number of input and test cases.     Now, find, what will be value for that position (row, col) in "by rows":         Look, in "by rows" method - a number will be increased sequentially in side of a row. For example:         consider 3x5 matrix -         1 2 3 4 5         6 7 8 9 10         11 12 13 14 15         there should have exact 15 numbers/values. So, what will be value of (...