# C++ Code for Primevilla (TCS Codevita) | PrepInsta

**Primevilla**

TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. **Primevilla** is one of the sample problem of this year TCS CodeVita season 11 competition.

**Question -: **It rains only on certain days in Primevilla. It is a rainy day when the number of days since a certain date is prime as well as the month is prime (i.e., month is one of Feb, Mar, May, Jul, Nov). Primevilla follows same calendar that we use.

Given the date d, identify if it would ever rain on a given weekday w within the next given n days. Also calculate the number of days r (where r > 0) after which it would rain within the next n days.

**Constraints**

2 <= n <= 10 ^ 10

**Input**

Input consists of three space delimited parts viz. <Date, DOW, n> where

- Date is of the format yyyymmdd
- DOW is Date of the Week in Ddd format
- n is a natural number (where n>=2)

**Output**

Output would be one of the three formats

- If it would rain on Ddd, r days after d within the next n days, print ‘Yes r’
- Else if p is the least prime > n such that it would rain on Ddd after p days, print ‘No p’
- Else print ‘No 0’, if it would never rain on Ddd.

**Time Limit (secs)**

1

**Examples :**

**Example 1**

**Input**

20211201 Sun 100

**Output**

Yes 67

**Explanation**

Starting from 20211201 we start checking for prime number days whether it would rain. The process will be as depicted below

20211201+2 is 20211203 Fri, month 12 is not prime —> It would not rain

20211201+3 20211204 Sat, month 12 is not prime —> It would not rain

..

20211201+31 is 20220101 Sat, month 1 is not prime —> It would not rain

..

20211201+67 is 20220206 Sun, month 2 is prime —> It would rain

Hence it could rain on a Sunday 67 days after the given date and within the given 100 days. Hence, output is “Yes 67”

**Example 2**

**Input**

20211201 Wed 100

**Output**

No 0

**Explanation**

The given date 20211201 is itself a Wednesday.

20211201+7 is 20211208 Wed, month 12 is not prime —> It would not rain

Also, any future Wednesday would be a multiple of 7 and hence non-prime days later than 20211201. So, it would never rain on future Wednesdays. Hence, output is “No 0”

**Example 3**

**Input**

19470815 Sat 150

**Output**

No 197

**Explanation**

If similar computation as depicted in prior examples is carried out one can figure out that it will rain on Sat, 28 Feb 1948 which is 197 days from 15 Aug 1947. However, since 197 is greater than 150 output is “No 197”.

#include < iostream> #include < map> #include < vector> #include < ctime> using namespace std; bool pr_fun(int h) { if (h <= 1) { return false; } for (int k = 2; k < h; k++) { if (h % k == 0) { return false; } } return true; } void gi(int h, vector& pre) { for (int k = 2; k <= h; k++) { if (pr_fun(k)) { pre.push_back(k); } } } int main() { string dye, dwk; int h; cin >> dye >> dwk >> h; vector pre; gi(365, pre); map dadic = {{0, "Mon"}, {1, "Tue"}, {2, "Wed"}, {3, "Thu"}, {4, "Fri"}, {5, "Sat"}, {6, "Sun"}}; struct tm t = {}; istringstream ss(dye); ss >> get_time(&t, "%Y%m%d"); time_t dye_time = mktime(&t); int das = -1; for (int k : pre) { time_t dte_time = dye_time + k * 24 * 60 * 60; struct tm* dte = localtime(&dte_time); if (pr_fun(dte->tm_mon + 1) && dadic[dte->tm_wday] == dwk) { das = k; break; } } if (das == -1) { cout << "No 0"; } else if (das <= h) { cout << "Yes " << das; } else { cout << "No " << das; } return 0; }

Login/Signup to comment