Educational Codeforces Round 98 (Rated for Div. 2) D. Radio Towers(斐波那契数列+概率)

题目链接:https://codeforc.es/contest/1452/problem/D

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.

Examples

input

2

output

748683265

input
3

output

748683265

input

5

output

842268673

input

200000

output

202370013

分析

n = 1 时,只有一种放法
n = 2 时,也只有一种方法
n = 3 时,有两种放法
n = 4时,有三种
n = 5时,有五种
……

我们发现分子就是斐波那契数列,分母则为 2 的 n 次方。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll mod = 998244353;
ll f[200007];
ll n;

ll qm(ll a,ll b)
{
	ll res = 1;
	while(b)
	{
		if(b & 1) res = (res * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return res % mod;
}

int main()
{
	f[1] = f[2] = 1;
	for(int i=3;i<=200000;i++) f[i] = (f[i - 1] + f[i - 2]) % mod;
	
	scanf("%lld",&n);
	ll ans = f[n] * qm(qm(2, n), mod - 2) % mod;
	printf("%lld",ans);
	
	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值