uva 1434 - YAPTCHA(数论)

本文介绍了解决UVA1434-YAPTCHA问题的方法,利用威尔逊定理进行快速计算,并通过筛选法预处理素数,实现高效解答。

题目链接:uva 1434 - YAPTCHA

题目大意:给定n和k,求题目中给定的式子S(n)。

解题思路:威尔逊定理,x为素数时有,((x1)!+1)%x==0,所以对于本题。假设3*k+7为素数的话,[(3k+6)!+1(3k+7[(3k+6)!3k+7]]=1

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int maxn = 1e6;

int ans[maxn+5], vis[maxn*4+5];

void primeTable (int n) {
    memset(vis, 0, sizeof(vis));

    for (int i = 2; i <= n; i++) {
        if (vis[i])
            continue;

        for (int j = 2 * i; j <= n; j += i)
            vis[j] = 1;
    }
}

int main () {
    primeTable(maxn*4);
    ans[1] = 0;
    for (int i = 2; i <= maxn; i++)
        ans[i] = ans[i-1] + (vis[3*i+7] ? 0 : 1);
    int cas, n;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d", &n);
        printf("%d\n", ans[n]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/mengfanrong/p/5170815.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值