HDU 2973 / UVa 1434 / CERC 2008 YAPTCHA (威尔逊定理及其逆定理)

本文介绍了一款帮助解决数学难题的程序设计解决方案,通过筛选素数和计算特定数学序列来解决问题。它适用于数学部门的网页测试,展示了将数学概念与编程结合的实际应用。

http://acm.hdu.edu.cn/showproblem.php?pid=2973

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=4180

Problem Description
The math department has been having problems lately. Due to immense amount of unsolicited automated programs which were crawling across their pages, they decided to put Yet-Another-Public-Turing-Test-to-Tell-Computers-and-Humans-Apart on their webpages. In short, to get access to their scientific papers, one have to prove yourself eligible and worthy, i.e. solve a mathematic riddle.


However, the test turned out difficult for some math PhD students and even for some professors. Therefore, the math department wants to write a helper program which solves this task (it is not irrational, as they are going to make money on selling the program).

The task that is presented to anyone visiting the start page of the math department is as follows: given a natural n, compute

where [x] denotes the largest integer not greater than x.
 

Input
The first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).
 

Output
For each n given in the input output the value of Sn.
 

Sample Input
  
13 1 2 3 4 5 6 7 8 9 10 100 1000 10000
 

Sample Output
  
0 1 1 2 2 2 2 3 3 4 28 207 1609
 

Source

思路:

n是合数时,((n-1)!+1)/n不为整数,所以在题目的和式中该部分为0;n是质数时就为1了。

所以筛一遍素数,在筛的过程中,找出形如3k+7的素数即可。


额外说一句,当a,b互素时,形如an+b的素数有无穷个。(wiki:Dirichlet's theorem on arithmetic progressions


完整代码:

/*796ms,8060KB*/

#include<cstdio>
#include<cmath>
const int mx = 3 * 1000000 + 7 + 1;
const int sqrt_mx = (int)sqrt((double)mx);
const int mx_n = 1000000 + 1;

bool a[mx_n], vis[mx];
int s[mx_n];

void init()
{
	int i, j;
	for (i = 2; i < mx; ++i)
	{
		if (!vis[i])
		{
			if ((i - 7) % 3 == 0) a[(i - 7) / 3] = true;
			if (i <= sqrt_mx)
				for (j = i * i; j < mx ; j += i) vis[j] = true;
		}
	}
}

int main()
{
	init();
	for (int i = 2; i < mx_n; ++i)
		s[i] = s[i - 1] + a[i];
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		printf("%d\n", s[n]);
	}
	return 0;
}


在洛谷网站上,有一些算法题涉及应用威尔逊定理,尤其在数论与素数判断相关的题目中。以下是一些典型的应用场景和题目示例: ### 三、威尔逊定理在洛谷上的相关题目 #### 1. **P5091 【模板】扩展欧拉定理** 该题虽然主要考察的是欧拉定理的扩展形式,但在某些解法中结合了阶乘模运算的性质,例如利用威尔逊定理处理 $ (p-1)! \mod p $ 的结果为 $ p - 1 $,从而优化某些组合数模素数的计算过程。 #### 2. **HDU 2973 YAPTCHA(可参考洛谷类似变种)** 这是一道典型的基于威尔逊定理的数学推导题。题目要求计算如下表达式: $$ \sum_{k=1}^{n}\left\lfloor\frac{(3k+7-1)!+1}{3k+7}-\left\lfloor\frac{(3k+7-1)!}{3k+7}\right\rfloor\right\rfloor $$ 通过令 $ x = 3k + 7 $,可以将原式转化为: $$ \sum_{k=1}^{n}\left\lfloor\frac{(x-1)!+1}{x}-\left\lfloor\frac{(x-1)!}{x}\right\rfloor\right\rfloor $$ 根据威尔逊定理,当 $ x $ 是素数时,$ (x-1)! \equiv -1 \mod x $,即 $ (x-1)! + 1 $ 能被 $ x $ 整除,此时上述表达式的值为 1;若 $ x $ 不是素数,则值为 0。因此该问题实质上是统计满足条件的素数个数[^1]。 ```cpp #include <iostream> using namespace std; typedef long long LL; const int N = 1000001; const int mx = 3000008; int s[N], p[N], vis[mx], t, n; void get_prime() { for (LL i = 2; i < mx; ++i) if (!vis[i]) { if ((i - 7) % 3 == 0) p[(i - 7) / 3] = 1; for (LL j = i * i; j < mx; j += i) vis[j] = 1; } } int main() { get_prime(); for (int i = 2; i < N; ++i) s[i] = s[i - 1] + p[i]; scanf("%d", &t); while (t--) { scanf("%d", &n); printf("%d\n", s[n]); } } ``` #### 3. **【模板】线性筛素数(P3383)** 虽然此题主要考察的是素数筛法,但其输出结果可用于验证威尔逊定理的正确性。例如,对于每个筛出的素数 $ p $,可以验证 $ (p-1)! + 1 $ 是否能被 $ p $ 整除。 --- ### 四、实际应用与编程技巧 在解决上述类型的问题时,通常会遇到数阶乘取模的问题。由于直接计算阶乘容易溢出,因此需要借助模逆元、分块预处理等技巧进行优化。例如,在模素数 $ p $ 情况下,可以预先计算阶乘模 $ p $,并结合费马小定理或扩展欧几里得算法求逆元,以高效实现组合数计算。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值