威尔逊定理 数论

本文探讨了威尔逊定理的应用,特别是在解决一个特定的数学难题中,即计算给定自然数n时Sn的值,Sn涉及到对3k+7形式的数进行判断是否为素数。通过实现一个辅助程序,我们能够高效地计算出任意给定n的Sn值,关键在于利用威尔逊定理快速判断素数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

威尔逊定理

n为素数时

(n - 1)! \equiv -1 (mod$ $n)

(n - 2) ! \equiv 1 (mod$ $n)

应用

\sum _{k=1}^{n} \left \lfloor \frac{(3 \cdot k + 6)! + 1}{(3 \cdot k+7)} +\left \lfloor \frac{(3 \cdot k + 6)!}{(3\cdot k + 7)} \right \rfloor \right \rfloor

YAPTCHA

 

YAPTCHA

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1858    Accepted Submission(s): 960


 

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

Central European Programming Contest 2008

 

 

Recommend

lcy

 

 

题解:3k + 7 为质数时, 又威尔逊定理左式等于0,右式等于-1 整式等于1

3k+7为合数时,(3k+6)!是3k +7的倍数所以\left \lfloor \frac{(3 \cdot k + 6)!}{(3\cdot k + 7)} \right \rfloor =\frac{(3 \cdot k + 6)!}{(3\cdot k + 7)},原式等于\left \lfloor \frac{1}{3k+7} \right \rfloor = 0

所以这题就是求给定集合的素数个数

/*
 *威尔逊定理
 */
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e6 +10;
const int maxm = 1e6 + 10;
int v[maxn];
vector <int> pri;
int cnt, n;
int ans[maxn];
void prime(){
    for (int i =2 ; i < maxn; i++){
        if (!v[i]) pri.push_back(i);
        for (int j = 0; j < pri.size() && pri[j] * i < maxn; j++){
            v[i * pri[j]] = 1;
            if (i % pri[j] == 0) break;
        }
    }
}

int main()
{
    prime();
    cnt = pri.size();
    int T;cin >> T;
    for (int i = 1; i < maxm; i++){
        int k = 3 * i + 7;
        if (!v[k]) ans[i] = ans[i - 1] + 1;
        else ans[i] = ans[i -1];
    }
    while (T--){
        scanf("%d", &n);
        cout << ans[n] << endl;
    }
    return 0;
}

 

### 关于 Codeforces 平台上与威尔逊定理相关的编程问题及解决方案 #### 威尔逊定理简介 威尔逊定理是一个重要的数论结论,它表明:如果 $p$ 是一个质数,则 $(p-1)! \equiv -1 \ (\text{mod}\ p)$。换句话说,$(p-1)! + 1$ 可被 $p$ 整除[^5]。这一性质在许多涉及质数检测和大数运算的算法竞赛题目中有广泛的应用。 --- #### 经典题目解析与实现 以下是几道典型的 Codeforces 题目以及它们基于威尔逊定理的解决方法: --- ##### **题目一:Codeforces 1295D** 该题的核心在于判断给定区间内有多少个数满足某种特殊条件,而这些条件可以通过威尔逊定理简化为对阶乘取模的结果分析[^6]。 ###### 解决方案 为了高效地处理大规模数据,我们可以预先计算出所有可能需要用到的阶乘值及其对应的模意义下的结果。具体代码如下所示: ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; ll factorial_mod(ll n, ll p) { if (n >= p) return 0; ll result = 1; for (ll i = 1; i <= n; ++i) { result = (result * i) % p; } return result; } bool is_prime_wilson(ll p) { if (factorial_mod(p - 1, p) != p - 1) return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll l, r; cin >> l >> r; int count = 0; for (ll i = max(l, 2LL); i <= r; ++i) { if (is_prime_wilson(i)) { count++; } } cout << count << "\n"; } ``` 这段程序首先定义了一个用于计算阶乘并对指定素数取模的功能函数 `factorial_mod`,接着借助这个工具实现了依据威尔逊定理判定某个自然数是否为素数的逻辑[^6]。 --- ##### **题目二:Prime Number Theorem Application** 虽然这不是一道具体的 Codeforces 题目名称,但它代表了一类常见的挑战场景——即如何利用包括但不限于威尔逊在内的各种经典数学理论去优化复杂度较高的枚举过程[^7]。 ###### 实现细节 当面临需要频繁验证多个候选数字是否属于素数集合的任务时,单纯依靠试除法往往难以达到理想的时间效率标准。此时引入诸如埃拉托斯特尼筛法或者更高级别的线性筛技术固然是一种不错的选择;然而,在某些特定条件下(例如仅需关心少量极大数值的情况),直接调用威尔逊定理反而能带来意想不到的优势。 示例代码片段展示如下: ```python def wilsons_theorem_test(n): from math import factorial if n < 2: return False elif pow(factorial(n - 1), 1, n) == n - 1: return True else: return False # Example Usage print(wilsons_theorem_test(11)) # Output: True ``` 在这里我们采用了 Python 内置库中的高精度整数运算能力来模拟整个流程,尽管如此仍需注意实际比赛中应尽量避免因过度依赖此类特性而导致性能瓶颈的发生[^7]。 --- #### 总结 综上所述,通过对威尔逊定理深入理解并灵活运用到不同类型的算法设计当中,不仅可以帮助参赛者更好地应对那些表面上看似棘手但实际上蕴含深刻规律可循的问题,而且还有助于培养更加严谨缜密的思维习惯! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值