codeforces997C
Sky Full of Stars
On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.
It is well known that one month on this planet consists of n2n2 days, so calendars, represented as square matrix nn by nn are extremely popular.
Weather conditions are even more unusual. Due to the unique composition of the atmosphere, when interacting with sunlight, every day sky takes one of three colors: blue, green or red.
To play the bingo, you need to observe the sky for one month — after each day, its cell is painted with the color of the sky in that day, that is, blue, green or red.
At the end of the month, students examine the calendar. If at least one row or column contains only cells of one color, that month is called lucky.
Let's call two colorings of calendar different, if at least one cell has different colors in them. It is easy to see that there are 3n⋅n3n⋅n different colorings. How much of them are lucky? Since this number can be quite large, print it modulo 998244353998244353.
Input
The first and only line of input contains a single integer nn (1≤n≤10000001≤n≤1000000) — the number of rows and columns in the calendar.
Output
Print one number — number of lucky colorings of the calendar modulo 998244353998244353
Examples
1
3
2
63
3
9933
Note
In the first sample any coloring is lucky, since the only column contains cells of only one color.
In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky:
While these colorings are not lucky:
有一个n×n的空白网格图,要求将每个格子染成红色、蓝色或者绿色,并且至少有一行或者一列的颜色相同。两种染色方案不同当且仅当至少有一个格子的染色不同。问不同的染色方案数。 n<=1e6

XJByy一下括号里的内容,3*(3n-i-1)n,其中3表示所有相同的横行的种类数,(3n-i-1)n,就是列不能是那种颜色的方案数,3i-3就是至少两个横行颜色不同的方案数,3n*(n-i)就是剩下格子随便填的方案数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
const ll Mod=;
ll n,fac[N],invf[N];
ll Ksm(ll x,ll y)
{
ll ans=1ll;
while(y)
{
if(y&) ans=ans*x%Mod; x=x*x%Mod; y>>=;
}
return ans;
}
inline void Ad(ll &x,ll y)
{
x+=y; x-=(x>=Mod)?Mod:; x+=(x<)?Mod:;
}
inline ll C(ll n,ll m)
{
return fac[n]*invf[m]%Mod*invf[n-m]%Mod;
}
int main()
{
// freopen("data.in","r",stdin);
ll i,ans;
R(n);
fac[]=1ll; for(i=;i<=n;i++) fac[i]=fac[i-]*i%Mod;
invf[n]=Ksm(fac[n],Mod-); for(i=n-;i>=;i--) invf[i]=invf[i+]*(i+)%Mod;
ans=Ksm(,n*n); Ad(ans,-Ksm((Ksm(,n)+Mod-)%Mod,n));
ll opt=-;
// cout<<"ans="<<ans<<endl;
for(i=;i<=n;i++)
{
ll Sum;
// cout<<n<<' '<<i<<' '<<C(n,i)<<endl;
Sum=opt*C(n,i)*(((*Ksm((Ksm(,n-i)-),n))%Mod+(Ksm(,i)-)*Ksm(,n*(n-i))%Mod)%Mod)%Mod;
Ad(ans,-Sum);
opt=-opt;
}
Wl(ans%Mod);
return ;
}
/*
Input
6
Output
977299444
*/
codeforces997C的更多相关文章
- Codeforces997C Sky Full of Stars 【FMT】【组合数】
题目大意: 一个$n*n$的格子,每个格子由你填色,有三种允许填色的方法,问有一行或者一列相同的方案数. 题目分析: 标题的FMT是我吓人用的. 一行或一列的问题不好解决,转成它的反面,没有一行和一列 ...
- codeforces997C Sky full of stars
传送门:http://codeforces.com/problemset/problem/997/C [题解] 注意在把$i=0$或$j=0$分开考虑的时候,3上面的指数应该是$n(n-j)+j$ 至 ...
- HDU5977 Garden of Eden 【FMT】【树形DP】
题目大意:求有所有颜色的路径数. 题目分析:参考codeforces997C,先利用基的FMT的性质在$O(2^k)$做FMT,再利用只还原一位的特点在$O(2^k)$还原,不知道为什么网上都要点分治 ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
随机推荐
- MQTT协议探究(二)
1 回顾与本次目标 1.1 回顾 MQTT控制报文的基本格式 WireShark进行抓包分析了报文 报文分析: CONNECT--连接服务器 CONNACK--确认连接请求 PINGREQ--心跳请求 ...
- MQTT图形化客户端比较
1 MQTT.fx (1)协议支持 TCP(tcp) TLS(tls) (2)特点 界面美观,操作便捷 不支持WebSocket协议 基于java开发 支持代理 通过Nashorn Engine的JS ...
- mysql replace substring 字符串截取处理
SELECT a1,a2,replace(a2, "豫ICP备16006180号-", "") a22,a3,a4,a5 FROM `aaab` order b ...
- No database provider has been configured for this DbContext
var context = ((IInfrastructure<IServiceProvider>)set).GetService<DbContext>(); 在EF Core ...
- mysql5.7 密码字段名更改
由password更改为authentication_string update user set authentication_string=password("123456") ...
- Linux更新程序脚本
DATE=$(date +%Y%m%d_%H%M%S) cd /opt/anystreaming/transcoder/ mv dll/libmonitor_service.so "dll/ ...
- script标签所应放的位置
一般放置的位置:<head>标签内,<body>标签内,<body>标签后(建议放在body标签后,利于页面的优化,优化页面结构加载的速度) 1.<head& ...
- bootstrap 处理警告
$("#id").bootstrapValidator({}).on('success.field.bv', function (e, data) { })
- ZPL语言完成条形码的打印
近期因为项目的需求,需要使用到打印机来打印业务相关的条形码和其他信息,由于之前有操作其它打印机的经验,Leader就安排我来做这个了(凑哦,这能说我是懵逼的么).于是就开始了我的探索之旅啦,不对,是踩 ...
- sql 随机数系列
一.把数据库把某个字段更新为随机数 DECLARE @Hour INT DECLARE @Counts INT SET @Hour =DATENAME(HOUR, GETDATE()) ) BEGIN ...