题面在这里!

题解见注释

/*设至少有i行,j列是一种颜色的方案数为 f(i,j)那么:1.i==0||j==0 时 , f(i,j) = C(n,i) * C(n,j) * 3^( (n-i)*(n-j) + i+j )2.否则 , f(i,j) = C(n,i) * C(n,j) * 3^( (n-i)*(n-j) + 1 )给上 f(i,j) 一个 (-1)^(i+j) 的容斥系数加起来 ((-1)^(i+j) = (-1)^(n-i+n-j)): 3 * C(n,i)*(-1)^(n-i) * C(n,j)*(-1)^(n-j) * (3^(n-i))^(n-j)3 * C(n,i)*(-1)^(n-i) * C(n,j) * (-3^(n-i))^(n-j)3 * C(n,i)*(-1)^(n-i) *  ( (1-3^(n-i))^n - (-1)^n * 3^((n-i)*n))考虑一种恰好有 I行,J列 是一种颜色的方案 , 它会在 f(i,j) 中被算 C(I,i) * C(J,j) 次.so 只要给 f(i,j) 一个 (-1)^(i+j) 的容斥系数 加起来得到 sum , 那么 sum 就是 没有一行且没有一列同色 (I+J==0) 的方案数了. 补集转化一下就是答案.
*/#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=998244353,N=1e6+5,mod=ha-1;inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;}inline int ksm(int x,int y){int an=1;for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;return an;
}int n,jc[N],ni[N],f[N],ans;inline void init(){jc[0]=1;for(int i=1;i<=n;i++) jc[i]=jc[i-1]*(ll)i%ha;ni[n]=ksm(jc[n],ha-2);for(int i=n;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
}inline int C(int x,int y){ return jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;}inline void solve(){ans=ksm(3,n*(ll)n%mod);for(int i=1,now;i<=n;i++){now=C(n,i)*(ll)ksm(3,(i+n*(ll)(n-i))%mod)%ha;if(i&1) ADD(ans,ha-add(now,now));else ADD(ans,add(now,now));}int tot=0;for(int i=1;i<=n;i++)if(n-i&1) ADD(tot,ha-C(n,i)*(ll)add(ksm(add(1,ha-ksm(3,n-i)),n),(n&1)?ksm(3,n*(ll)(n-i)%mod):ha-ksm(3,n*(ll)(n-i)%mod))%ha);else ADD(tot,C(n,i)*(ll)add(ksm(add(1,ha-ksm(3,n-i)),n),(n&1)?ksm(3,n*(ll)(n-i)%mod):ha-ksm(3,n*(ll)(n-i)%mod))%ha);/*for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i+j&1) ADD(tot,ha-C(n,i)*(ll)C(n,j)%ha*(ll)ksm(3,(n-i)*(ll)(n-j)%mod));else ADD(tot,C(n,i)*(ll)C(n,j)%ha*(ll)ksm(3,(n-i)*(ll)(n-j)%mod));*/ADD(ans,tot*3ll%ha);ans=add(ha-ans,ksm(3,n*(ll)n%mod));
}int main(){scanf("%d",&n),init();solve(),printf("%d\n",ans);return 0;
}

转载于:https://www.cnblogs.com/JYYHH/p/9259491.html

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. CF 997C Sky Full of Stars

    传送门 题目大意 思路 参考代码 总结 传送门 题目大意 有一个 n×n(n≤106)n×n(n≤106)n \times n \pod {n \le 10^6} 的正方形网格.用三种颜色对每个格子染 ...

  4. 【CodeForces 997C】Sky Full of Stars(组合计数)

    题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...

  5. Codeforces 997 C - Sky Full of Stars

    C - Sky Full of Stars 思路: 容斥原理 题解:http://codeforces.com/blog/entry/60357 注意当i > 1 且 j > 1,是同一种 ...

  6. 数论五之容斥——硬币购物,Gerald and Giant Chess,幸运数字,Sky Full of Stars,已经没有什么好害怕的了

    容斥的神 [HAOI2008]硬币购物 problem solution code CF559C Gerald and Giant Chess problem solution code [SCOI2 ...

  7. cf997C. Sky Full of Stars(组合数 容斥)

    题意 题目链接 \(n \times n\)的网格,用三种颜色染色,问最后有一行/一列全都为同一种颜色的方案数 Sol Orz fjzzq 最后答案是这个 \[3^{n^2} - (3^n - 3)^ ...

  8. CF997C Sky Full of Stars

    CF997C , Luogu 有一个 \(n \times n ( n \leq 10^6)\)的正方形网格,用红色,绿色,蓝色三种颜色染色,求有多少种染色方案使得至少一行或一列是同一种颜色.结果对 ...

  9. CF997C Sky Full of Stars 数论

    正解:容斥 解题报告: 传送门! 两个方法,分别港下QAQ 先说第一种 首先要推出式子,就∑2*C(i,n)*(-1)i+1*3i*3n*n-n+3*∑∑(-1)i+j+1*C(i,n)*C(j,n) ...

最新文章

  1. php处理异步请求_PHP实现异步调用方法研究
  2. php 匿名方法,PHP基于Closure类创建匿名函数的方法详解
  3. 【学术相关】11 个好用的科研工具推荐!工作效率提升 max!
  4. 煤矿安全规程专家解读2016电子版_【学习】煤矿安全规程专家解读(165)
  5. Android 模拟器调试的缺点
  6. 【小记录】关于dojo中的on事件
  7. UVA10838 The Pawn Chess
  8. plsql登录空白框_王者荣耀空白居中以及重复名的教程[含软件]
  9. 关于WIN10显示“未安装任何音频输出设备” 英特尔(R)智音系统OED启动错误(代号10)解决办法
  10. S32K MCAL02-FlexCAN 时钟模块【理论部分】
  11. HTML+CSS+JavaScript仿京东购物商城网站 web前端制作服装购物商城 html电商购物网站...
  12. ie浏览器总跳转到 http://hao.360.cn
  13. 安装SQL server2017提示无法打开注册表项:“Software\Microsoft\MicrosoftsQL Server\140\Bootstrap“,注册表可能已损环。
  14. 欢迎段海华——我们开发者社区中文版的新版主!
  15. 子网掩码的作用和用法
  16. 神武3进不去 服务器响应,windows7系统玩神武2卡机的解决方法
  17. 横向越权与纵向越权区别
  18. 百度大脑EasyDL多人标注重磅上线啦
  19. 打印系统开发(27)——WinForm开发(49)——c# winform 打印图像,PrintDialog打印并且设置打印纸张大小
  20. CUMT 1031 Order

热门文章

  1. 共享本人收藏的php编程软件
  2. 量产教程-(ut165)
  3. java自动生成略缩图
  4. 273、隐藏个人信息
  5. 闪存驱动器_在任何Windows计算机上从您的闪存驱动器运行便携式Chrome
  6. bitlocker加密时间_BitLocker To Go加密Windows 7中的便携式闪存驱动器
  7. 金多多配资提示大概率会选择方向
  8. 12个有用的网站工具
  9. 商业模式案例|瑞幸咖啡的内容营销策略
  10. 4. Matplotlib设置线型风格