HDU6814 Tetrahedron(几何/数论/逆元)

Generate three integers a, b, and c in [1,n] with equal probability independently, and use them as the three right-angle side length of a right-angled tetrahedron. Find the expectation of the reciprocal square of the distance from the right-angle apex to the slope (Euclidean distance).

For each test case, output a line containing the answer mod 998244353.

 ![img](https://vj.z180.cn/bc0539328995156faa5623ceb3e4ea0c?v=1596532892)   

Input

In the first line, you should read an integer T denoting the number of test cases.

In every test case, the only line will include an integer n.

It is guaranteed that T is no larger than 2×106 and n is no larger than 6×106.

Output

For each test case, output the only line containing just one integer denoting the answer mod 998244353.

Sample Input

3
1
2
3

Sample Output

3
124780546
194103070

题意是从1~n等概率任选3个数,求以这三个数为直角边构成的直角三棱锥中直角顶点到斜平面的距离平方的倒数的期望。

由高中数学可得有公式1OH2=1a2+1b2+1c2,这个可以设出来三条边然后根据海伦公式推。那么最终要求的期望E就为E(1OH2)=3×E(1a2)=3×1n3×Σi=1nΣj=1n21i2=3n×Σi=1n1i2

然后预处理出1~6e6的1i2的前缀和,查询的时候直接输出即可。但这里还有取模操作,分数取模用到费马小定理,ab%p=a×bp2%p,用快速幂搞一下就好了。

重点中的重点:i×i后也要取模!WA了15发就是没注意....

#include <bits/stdc++.h>
#define mod 998244353
typedef long long ll;
using namespace std;
long long ans[6000005]; 
long long ksm(long long x,long long y){
    long long a=1;
    while(y){
        if(y&1) a=a*x%mod;
        y>>=1;
        x=x*x%mod;
    }
    return a;
}
int main()
{
	//freopen("A.in","r",stdin);
	//freopen("my.out","w",stdout);
	int t;
	cin >> t;
	ans[1] = 1;

	for(long long i = 2; i <= 6000005; i++)
	{
		ans[i] = ksm(i * i % mod, mod-2) % mod;
		ans[i] = (ans[i - 1] % mod + ans[i] % mod) % mod;
	}
	while(t--)
	{
		long long n;
		scanf("%lld", &n);
		printf("%lld\n", ans[n] * 3 * ksm(n , mod-2) % mod);
	}
	return 0;
}  
posted @   脂环  阅读(205)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩