威尔逊定理

威尔逊定理

当p为质数时 (p-1)! ≡ -1(mod p)

在这里插入图片描述
此题输入一个数n求解Sn,就是运用了威尔逊定理
其中[x]的含义是取不大于x的最小整数
那么 当3k+7为质数时,发现那一筐值为1;当3k+7不为质数时,那一筐值为0

代码如下

#include
#include
#include
using namespace std;
const int maxn=1e6+1;
int s[maxn];
int sushu(int n)
{
	if(n==1) return 0;
	for(int i=2;i<=sqrt(n);i++)
	{
		if(n%i==0)
		return 0;
	}
	return 1;
}
void init()
{
	s[0]=0;
	for(int i=1;i<maxn;i++)
	{
		if(sushu(3*i+7)==1)
		s[i]=s[i-1]+1;
		else
		s[i]=s[i-1];
	}
}
int main()
{
    init();
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%d\n",s[n]);
    }
    return 0;
}

你可能感兴趣的:(威尔逊定理)