hihoCoder挑战赛28 题目3 : 树的方差
2024-08-27 21:40:41
题目3 : 树的方差
时间限制:20000ms
单点时限:1000ms
内存限制:256MB
描述
对于一棵 n 个点的带标号无根树,设 d[i] 为点 i 的度数。
定义一棵树的方差为数组 d[1..n] 的方差
给定 n ,求所有带标号的 n 个点的无根树的方差之和。
你需要将答案对 998244353 取模。
方差的定义:https://en.wikipedia.org/wiki/Variance
输入
仅一行:一个正整数 n
2 ≤ n ≤ 106
输出
仅一行:一个非负整数表示答案
样例解释
3个点的无根树有3种,每种的度数分布都是[1,2,1]
于是方差=((1-(4/3))2+(2-(4/3))2+(1-(4/3))2)/3=2/9
由于有3种,所以答案是2/3
分数取模的方法:https://math.stackexchange.com/questions/586595/finding-modular-of-a-fraction
- 样例输入
-
3
- 样例输出
-
665496236
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const ll mod=;
ll fpow(ll a,ll p){
ll res=;
for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
return res;
}
ll n,tem,ans;
int main(){
cin>>n;
ans=(n-)*(n-)%mod;
if(n<=){
tem=fpow(n,mod-);
ans=ans*tem%mod;
cout<<ans<<'\n';
return ;
}
tem=fpow(n,n-);
ans=ans*tem%mod;
cout<<ans<<'\n';
return ;
}
最新文章
- MyBatis通过JDBC生成的执行语句问题
- Akka框架使用注意点
- 【leetcode】Remove Duplicates from Sorted List (easy)
- 如何在Linux命令行中创建以及展示演示稿
- ASP运行流程(主要的类笔记)
- 连续子数组的最大和/1007. Maximum Subsequence Sum (25)
- 修改过mysql数据库字段内容默认值为当前时间
- 给定N个整数集合是否存在两个其和刚好为指定常数的元素
- Android圆形图片自定义控件
- ZS and The Birthday Paradox
- Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)
- git上传项目到github简易步骤
- python--多继承
- org.apache.ibatis.binding.BindingException: Type interface XXX is not known to the MapperRegistry.
- HDU 1431 素数回文 离线打表
- jzoj2941
- Python处理PDF和Word文档常用的方法(二)
- Python 字节码是什么
- pip离线安装
- Coursera课程《Python数据结构》中课程目录
热门文章
- Unity3D Shader基础教程
- 在Android平台下搭建PhoneGap开发环境--用HTML5开发游戏
- Objective-C语法之NSDictionary和NSMutableDictionary
- 《HTTP权威指南》学习笔记——HTTP报文
- Windows 安装 adt-bundle的方法
- 妙味远程课堂-JS热身运动-上
- SpringMVC -- 梗概--源码--贰--RestFul收参(了解) @PathVariable
- javascript 显示一定范围内的素数(质数)
- 把mongodb服务添加到系统服务中,报错:[sc] openscmanager 失败 5
- Linux Redis安装,Linux如何安装Redis,Linux Redis自动启动,Redis开机启动