Loading

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 \times 10^6\) and \(n\) is no larger than \(6 \times 10^6\).

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个数,求以这三个数为直角边构成的直角三棱锥中直角顶点到斜平面的距离平方的倒数的期望。

由高中数学可得有公式\(\frac{1}{OH^2}=\frac{1}{a^2}+\frac{1}{b^2}+\frac{1}{c^2}\),这个可以设出来三条边然后根据海伦公式推。那么最终要求的期望E就为\(E(\frac{1}{OH^2})=3\times E(\frac{1}{a^2})=3\times \frac{1}{n^3}\times\Sigma^n_{i=1}\Sigma^{n^2}_{j=1}\frac{1}{i^2}=\frac{3}{n}\times \Sigma^{n}_{i=1}\frac{1}{i^2}\)

然后预处理出1~6e6的\(\frac{1}{i^2}\)的前缀和,查询的时候直接输出即可。但这里还有取模操作,分数取模用到费马小定理,\(\frac{a}{b}\%p=a\times b^{p-2}\%p\),用快速幂搞一下就好了。

重点中的重点:\(i\times 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 @ 2020-08-05 16:35  脂环  阅读(199)  评论(0编辑  收藏  举报