• 威尔逊定理:
    • 判定一个自然数是否为素数充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p )(即( p -1 )! ≡p -1 ( mod p ))
    • 推论:当且仅当p为素数时,(p-1)!+1能被p整除
  • HDU2973
  • 题意
    • 给出n计算
  • 题解
    • 显然,因为3k+7>1,所以外括号内的数只能在(0,1] 的范围之内,向下取整后只有0,1两种情况。
    • 当且仅当前半部分为整数时,外括号内的数为一,即取1,否则取0
    • 将3k+7当作p,运用威尔逊定理,当且仅当p为素数时,答案累加1
  • 代码
    • //多次输入,需要素数筛和前缀和
      //#include<bits/stdc++.h>
      #include<algorithm>
      #include <iostream>
      #include  <cstdlib>
      #include  <cstring>
      #include  <cassert>
      #include   <cstdio>
      #include   <vector>
      #include   <string>
      #include    <cmath>
      #include    <queue>
      #include    <stack>
      #include      <set>
      #include      <map>
      using namespace std;
      #define P(a,b,c) make_pair(a,make_pair(b,c))
      #define rep(i,a,n) for (int i=a;i<=n;i++)
      #define per(i,a,n) for (int i=n;i>=a;i--)
      #define CLR(vis) memset(vis,0,sizeof(vis))
      #define MST(vis,pos) memset(vis,pos,sizeof(vis))
      #define pb push_back
      #define mp make_pair
      #define all(x) (x).begin(),(x).end()
      #define fi first
      #define se second
      #define SZ(x) ((int)(x).size())
      typedef pair<int,pair<int,int> >pii;
      typedef long long ll;
      typedef unsigned long long ull;
      const ll mod = 1000000007;
      const int INF = 0x3f3f3f3f;
      ll gcd(ll a, ll b) {
      	return b ? gcd(b, a%b) : a;
      }
      template <class T>
      inline bool scan_d(T &ret) {
      	char c;
      	int sgn;
      	if(c=getchar(),c==EOF) return 0; //EOF
      	while(c!='?'&&(c<'0'||c>'9')) c=getchar();
      	sgn=(c=='?')?-1:1;
      	ret=(c=='?')?0:(c-'0');
      	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
      	ret*=sgn;
      	return 1;
      }
      inline void outt(int x) {
      	if(x>9) outt(x/10);
      	putchar(x%10+'0');
      }
      template<class T>inline void gmax(T &A, T B){
      	(A<B)&&(A=B);//if(B>A)A=B;
      }
      template<class T>inline void gmin(T &A, T B){
      	(A>B)&&(A=B);//if(B<A)A=B;
      }
      const int maxn=4e6+10;
      int a[maxn];
      int vis[maxn];
      int cnt[maxn];
      void prim(){
      	vis[0]=1;
      	vis[1]=1;
      	rep(i,2,4000000){
      		if(!vis[i]){
      			for(int j=i*2;j<=4000000;j+=i){
      				if(!vis[j])vis[j]=1;
      			}
      		}
      	}
      }
      
      
      
      
      int main(){
      	int t;
      	cin>>t;
      	prim();
      	rep(i,1,1000000){
      		if(!vis[i*3+7])cnt[i]=cnt[i-1]+1;
      		else cnt[i]=cnt[i-1];
      	}
      	while(t--){
      		int n;
      		scanf("%d", &n);
      		printf("%d\n", cnt[n]);
      	}
      	return 0;
      } 
      

发表评论

邮箱地址不会被公开。 必填项已用*标注