网页链接:传送门

C. Sky Full of Stars
time limit per test : 4 seconds
memory limit per test : 256 megabytes
input : standard input
output : standard output

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 n2 days, so calendars, represented as square matrix n by n 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⋅n different colorings. How much of them are lucky? Since this number can be quite large, print it modulo 998244353.
Input
The first and only line of input contains a single integer n (1≤n≤1000 000) — the number of rows and columns in the calendar.
Output
Print one number — number of lucky colorings of the calendar modulo 998244353

Input Output
1 3
Input Output
2 63
Input Output
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:

分析

题意:用 333 种颜色对 n∗nn*nn∗n 个格子染色,问至少有一行或一列只有一种颜色的方案数是多少。

任意染色的方案数是 3n23^{n^2}3n2 种(共有 n∗nn*nn∗n 个格子,每个格子有 333 种情况),我们只需要考虑不满足题目的条件的情况有多少种,与任意情况的数量相减,即可得到满足题目条件的情况数量。

以下分为两种大情况:

  1. 仅考虑没有 全部是一种颜色的
    对于任意一列来说,该列有 nnn 个格子,因此该列任意染色的方案数是 3n3^n3n 种,去掉满足题目条件(在这里是每一列只有一种颜色)的情况数 333 ,每列就有 3n−33^n-33n−3 种情况是不满足题目条件的。那么 nnn 列,就共有 (3n−3)n(3^n-3)^n(3n−3)n 种。

  2. 仅考虑没有 全部是一种颜色的
    如果有 iii 行只有一种颜色,其他行任意填。因为其他行是任意填的,可能就会出现另外一个只有一种颜色的行,即会出现重复计数的情况。因此我们需要利用 容斥原理 来计数。
    公式: ∑i=1n(−1)iCnif(i)\sum_{i=1}^{n}(-1)^iC_n^if(i)∑i=1n​(−1)iCni​f(i) 。其中 CniC_n^iCni​ 的意思是从 nnn 行中选出 iii 行,(−1)i(-1)^i(−1)i 则是用来容斥(奇数个加,偶数个减。这里反了一下是因为最后计算的时候最前面会加负号),f(i)f(i)f(i) 代表有 iii 行颜色相同的方案的个数。
    下面我们来推导 f(i)f(i)f(i) 的值。注意我们不能产生只有一种颜色的列,只能产生只有一种颜色的行(因为列的情况我们上面已经考虑过了)

    1. 这 iii 行颜色相同
      我们需要从 333 种颜色中选一种给 iii 行上色,对于剩下的长度为 n−in-in−i 的 nnn 小列,我们进行任意涂色。但要注意不能和上面的 iii 行颜色一致,否则会出现有颜色相同的列,这样就和第一种大情况重复了。所以每列的方案数是 3n−i−13^{n-i}-13n−i−1(这个 111 是指和 iii 行颜色相同的情况), nnn 列的方案数是 (3n−i−1)n(3^{n-i}-1)^n(3n−i−1)n ,那么总方案数就是 3(3n−i−1)n3(3^{n-i}-1)^n3(3n−i−1)n (最前面的 333 是因为 iii 行颜色相同有三种情况)。
    2. 这 iii 行颜色不同
      如果这 iii 行颜色不同,那就说明 iii 行有 3i−33^i-33i−3 种方案(后面的 333 是 iii 行颜色相同时的三种情况),剩下的 n(n−i)n(n-i)n(n−i) 个格子任意涂色,可以保证不会产生颜色相同的列。因此总方案数是 3n(n−i)(3i−3)3^{n(n-i)}(3^i-3)3n(n−i)(3i−3) 。

    综上所述: f(i)=3(3n−i−1)n+3n(n−i)(3i−3)f(i)=3(3^{n-i}-1)^n+3^{n(n-i)}(3^i-3)f(i)=3(3n−i−1)n+3n(n−i)(3i−3) 。

综上所述*2:最后的结果为 ans=3n2−(3n−3)n−∑i=1n(−1)iCni[3(3n−i−1)n+3n(n−i)(3i−3)]ans=3^{n^2}-(3^n-3)^n-\sum_{i=1}^{n}(-1)^iC_n^i[3(3^{n-i}-1)^n+3^{n(n-i)}(3^i-3)]ans=3n2−(3n−3)n−∑i=1n​(−1)iCni​[3(3n−i−1)n+3n(n−i)(3i−3)]

直接把组合计数的板子cv上去会超时,所以需要稍微改动一下。

其实也没那么那么险⑧(小声bb)

代码

#include<cstdio>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll ksm(ll a,ll b)   //快速幂
{ll res=1;while(b){if(b&1) res=res*a%mod;a=a*a%mod;b>>=1;}return res;
}
int main()
{ll n,ans,c=1;int k=-1;scanf("%lld",&n);ans=(ksm(3,n*n)%mod-ksm(ksm(3,n)-3,n)+mod)%mod;for(int i=1;i<=n;i++){k=-k;c=c*(n-i+1)%mod;c=c*ksm(i,mod-2)%mod;//上一步及这一步是利用逆元组合计数,如果在外面写个自定义函数去算会超时(当然也可能是我用的有问题)ans=(ans+k*c%mod*(3*ksm(ksm(3,n-i)-1,n)%mod+ksm(3,n*(n-i))%mod*(ksm(3,i)-3)%mod))%mod;}printf("%lld",(ans+mod)%mod);return 0;
}

参考

巨巨的链接

CodeForces 997C-Sky Full of Stars〖容斥定理+组合计数〗相关推荐

  1. [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)

    [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) ...

  2. CodeForces 997C Sky Full of Stars

    题目:点击打开链接 题意:给n*n个格子,每个格子可以填3种颜色.问有多少种填色方案,至少有一列或一行是同色的. 分析:首先反过来考虑,找不幸运的组合,因为这样便于容斥.  要把列和行分开考虑.  首 ...

  3. CodeForces - 997C Sky Full of Stars

    题面在这里! 题解见注释 /*设至少有i行,j列是一种颜色的方案数为 f(i,j)那么:1.i==0||j==0 时 , f(i,j) = C(n,i) * C(n,j) * 3^( (n-i)*(n ...

  4. SCU - 4573 和 POJ 3904 Sky Code 【思维 + 容斥定理 or 反演】

    传送门 题意: 就是给定n个数, 问这里面有多少个四元组满足gcd(a, b, c, d) == 1. 思路: 因为题意的那个条件并不是说一定这四个元素就是两两互质的, 所以算正面是有点难算的, 所以 ...

  5. #容斥,组合计数#洛谷 3214 卡农

    题目 在集合S=[1∼n]S=[1\sim n]S=[1∼n]中选出mmm个子集,满足三点性质: 所有选出的mmm个子集都不能为空. 所有选出的mmm个子集中,不能存在两个完全一样的集合. 所有选出的 ...

  6. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  7. hdu 1796 How many integers can you find 容斥定理

    一开始看 这里 这个文章博主写得很好. 当举容斥定理的所谓 奇数为负 偶数为正的时候. 我直接就认为是 a*b 了.实际上是lcm(a,b). 由于博文中的因子都是互素的(素数之间).所以lcm(a, ...

  8. UVA-10212 The Last Non-zero Digit. 分解质因子+容斥定理

    这个是参考了别人之后的代码,POJ上0MS过了.Orz......对于一个序列在提取了2,5之后,例如1,2,3,4,5,6,7,8,9,10,我们可以将其中的奇数和偶数分开来对待,对于偶数序列2,4 ...

  9. HDU-4059 The Boss on Mars 容斥定理

    将与N不互质的数全部找出来,再应用容斥定理求得最后的结果.这题在求 a/b MOD c 的时候使用费马小定理等价于 a*b^c(-2) MOD c.用x/lnx 可以估计出大概有多少素数. 代码如下: ...

最新文章

  1. java旋转图片并画出_java实现图片角度旋转并获得图片信息
  2. 多线程编程之四——线程的同步
  3. android intent参数是上次的结果,【Android】7.0 Intent向下一个活动传递数据、返回数据给上一个活动...
  4. BugkuCTF-MISC题MuMuMisc的简单题
  5. centos php 开启libgdgd_CentOS6.5安装Nginx1+MySQL5+PHP5
  6. isupper_Python字符串isupper()
  7. mysql集群 自增_为什么我们要从MySQL迁移到TiDB?
  8. T-SQL连接查询,基础连接理解
  9. 教你ogg怎么转mp3格式
  10. python爬取b站番剧链接,Scrapy爬虫爬取B站视频标题及链接
  11. 学大数据应该会什么?
  12. 白帽SEO为什么更好?
  13. 怎样在vs2010中添加图片资源呢?
  14. 《Wasserstein GAN》继续 GAN
  15. 微信小程序云开发 · 从0搭建商业级校园跑腿平台(已开源)
  16. 快手亮相第七届全球数字营销峰会,以全域内容营销驱动商业新增长
  17. 【手把手反内卷】开创全新AI多模态任务一视听分割:代码实践、优化教程(二)
  18. 项目中分页查询得实现
  19. android蓝牙连接取消后怎么重新连上,重新启动后接收蓝牙连接更改
  20. ping请求超时的解决方法?

热门文章

  1. 【postgres】5、pgbackrest 冷备份介绍、命令、脚本
  2. 零差云控(zeroerr)eCoder35S-R20B开箱
  3. SCRM会员营销管理软件哪家比较好?怎么选择SCRM会员营销系统?
  4. 解析搜狗词库(python)
  5. Media Player Classic - HC 源代码分析 8:RenderFile函数详细分析(CFGManager)
  6. 如何在hdfs上将文件下载_如何在Windows 10上将文件复制到USB闪存驱动器
  7. 电脑传图片到手机adb命令
  8. 重庆集中签约1300多亿元PPP项目 新增智慧城市等项目
  9. 情人节程序员用HTML网页表白【七夕活动邀请函手机动态模板】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
  10. 关于Visual Studio Code下载速度慢问题