Educational Codeforces Round 98 (Rated for Div. 2)------Radio Towers

题目

There are n+2 towns located on a coordinate line, numbered from 0 to n+1. The i-th town is located at the point i.

You build a radio tower in each of the towns 1,2,…,n with probability 12 (these events are independent). After that, you want to set the signal power on each tower to some integer from 1 to n (signal powers are not necessarily the same, but also not necessarily different). The signal from a tower located in a town i with signal power p reaches every city c such that |c−i|<p.

After building the towers, you want to choose signal powers in such a way that:

towns 0 and n+1 don’t get any signal from the radio towers;
towns 1,2,…,n get signal from exactly one radio tower each.
For example, if n=5, and you have built the towers in towns 2, 4 and 5, you may set the signal power of the tower in town 2 to 2, and the signal power of the towers in towns 4 and 5 to 1. That way, towns 0 and n+1 don’t get the signal from any tower, towns 1, 2 and 3 get the signal from the tower in town 2, town 4 gets the signal from the tower in town 4, and town 5 gets the signal from the tower in town 5.

Calculate the probability that, after building the towers, you will have a way to set signal powers to meet all constraints.

Input
The first (and only) line of the input contains one integer n (1≤n≤2⋅105).

Output
Print one integer — the probability that there will be a way to set signal powers so that all constraints are met, taken modulo 998244353.

Formally, the probability can be expressed as an irreducible fraction xy. You have to print the value of x⋅y−1mod998244353, where y−1 is an integer such that y⋅y−1mod998244353=1.

题意

有1-n个村庄,每个村庄可以有1/2的概率设置一个信号塔,信号塔的覆盖范围可以自己设定为x,请保证1-n的村庄有信号,而0和n + 1的村庄不可以有信号,计算建造好信号塔之后,你将有一种方法设置信号功率来满足所有村庄的概率。要求,每个村庄只能被一个信号所覆盖,意思就是,两个信号塔的覆盖范围不可以有交叉

题解

n = 1 1/2(1种方法)
n = 2 1/4(1种方法)
n = 3 2/8(2种方法)
n = 4 3/16(3种方法)
·······
······
·····
大胆猜测:
继续往下推会发现,分子斐波那契数列,分母为2的n次

AC代码

#include <iostream>
using namespace std;
#define ll long long
const ll mod = 998244353;
const int N = 2e5 + 15;
//扩展欧几里得算法,其时间复杂度与辗转相除法相同
ll extgcd(ll a,ll b,ll &x,ll &y){
    ll ans=a;
    if(b!=0){
        ans=extgcd(b,a%b,y,x);
        y-=(a/b)*x;
    }
    else{
        x=1;
        y=0;
    }
    return ans;				//ans为gcd(a,b)
}
//模a关于模m的乘法逆元
ll mod_inverse(ll a,ll m){
    ll x,y;
    extgcd(a,m,x,y);
    return (x%m+m)%m;		//如果是负数则要转化成正数
}
ll f[N];
void init(){
    f[1] = f[2] = 1;
    for(int i = 3; i < N; i++){
        f[i] = (f[i - 1] + f[i - 2]) % mod;
    }
}
ll quick_pow(ll x, ll y){
    int res = 1;
    while(y){
        if(y&1){
            res = (res * x) % mod;
            y--;
        }
        y /= 2;
        x = (x * x) %mod;
    }
    return res;
}
int main()
{
    ll n; cin >> n;
    init();
    ll x = quick_pow(2, n);
    cout << ((f[n] % mod) * (mod_inverse(x, mod) % mod)) % mod <<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值