Problem statement
India has a long and glorious history. There are ‘N’ + 2 cities located in a line in India, numbered from 0 to 'N' + 1. The i-th city is located at point ‘i’. Since elections are coming and Indian politicians are working on their peak to prove their people their dominance. There is a lot of work that needs to be done in the telecom industry. But before that first dry run is very important on machines, only then engineers can shift to live implementation in the cities. You are given a task to write a computer program to visualise the stuff. The problem in detail is given below.
You build a telecom tower in each of the cities 1,2,…,'N' with a probability of 1/2 (and all these events are independent). After that, you'll want to set the signal power on each tower to a number between 1 and 'N' . (Signal powers aren't always the same, but they're also not always different.). The signal from a telecom tower located in a city ‘i’ with signal power 'power' reaches every city c such that |c−i|<p|c−i|<power.
After building the telecom towers, you have to choose the signal powers in the given way:
- city 0 and 'N' +1 don't get any signal from the telecom towers;
- city 1,2,…,'N' get signals from exactly one telecom tower each.
Let's suppose, if N=5, and you have built the telecom towers in cities 2, 4, and 5, then you may set the signal power of the telecom tower in city 2 to 2, and the signal power of the telecom towers in cities 4 and 5 to 1. That way, cities 0 and 'N' +1 don't get the signal from any telecom tower, cities 1, 2, and 3 get the signal from the telecom tower in city 2, city 4 gets the signal from the telecom tower in city 4, and city 5 gets the signal from the telecom tower in city 5.
Calculate the probability that, after building the telecom towers, you will have a way to set signal powers to meet all constraints. The input comprises one number n, where n is less than 10^5.Print only one integer, which has the value of the probability that there will be a way to set signal powers such that all the constraints are satisfied. Also, take the modulo of answer with 998244353998244353.
Example 1
Input
2
Output
748683265
Explanation
In this example, the actual answer is ¼ with probability ¼, the telecom towers are built in both cities 1 and 2, so we can adjust their signal powers to 1.
Example 2
Input
5
Output
842268673
Explanation
The actual answer for this test case is 5/32. Note that even though in the previous test case explanations we used equal signal powers for all telecom towers, it is not necessary. So let's suppose, if N = 5 and the telecom towers are built in cities 2, 4 and 5, then you may adjust the signal power of the telecom tower in cities 2 to 2, and set the signal power of the telecom towers in towns 4 and 5 to 1.
Example 3
Input
42
Output
708668919
Example 4
Input
6
Output
873463809
Also see, Euclid GCD Algorithm
Approach
To solve this problem “Telecom Towers in a city” you should have a great grasp of the number theory concepts. So, we can solve this problem using the assumption technique and some basic observations. The crucial observation is that when the positions of telecom towers are fixed, the way to set their signal powers is unique if it exists. That's because the first telecom tower should have its signal power exactly equal to the required to cover all cities before it, the second tower should have signal power exactly equal to the required to cover all cities before it that wasn't covered by the first one, and so on. So let's make a count of the total number of ways to cover all cities, and then divide it by 2 ^ N. Covering all cities can be expressed as splitting 'N' into the sum of several positive odd integers. We will iterate for all cities and will calculate the answer simultaneously. We can optimize this approach by using dynamic programming which precomputes the answer for the smaller test case.
Code:
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long quickpow(long long a, long long b) {
long long res = 1;
a %= mod;
while (b) {
if (b & 1)
res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
long long inv(long long n) {
return quickpow(n, mod - 2);
}
long long c(int n, int m) {
long long res = 1;
for (int i = 1; i <= m; ++i) {
res = res * (n + 1 - i) % mod;
res = res * inv(i) % mod;
}
return res;
}
int main() {
// freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
long long ans = 0;
if (n % 2) {
for (int i = 1; i <= n; i += 2) {
ans = (ans + c((n + i) / 2 - 1, i - 1)) % mod;
}
}
else {
for (int i = 2; i <= n; i += 2) {
ans = (ans + c((n + i) / 2 - 1, i - 1)) % mod;
}
}
printf("%lld\n", ans * inv(quickpow(2, n)) % mod);
return 0;
}
Output
TLE




