题目大意:
给定一个自然数 n,计算
其中[x]表示不大于x的最大整数。
思路:
额,某位dalao告诉我这道题是威尔逊定理 但是我还是不知道那是啥。到网上搜了一下,威尔逊定理是这样的:
当且仅当pp为素数时,(p−1)!≡−1(mod p)
证明我当然是不会的,详情看这里
所以,如果这道题3k+7为质数,那么(3k+6)!+1/3k+7就正好大于[(3k+6)!/3k+7],所以相减就为1,否则这两个数整数部分相同,差为0。所以,这道题就先预处理出小于等于106的n的答案,再O(1)输出即可。
代码: #include <cstdio> #define N 1000010 using namespace std; int a[N+10],t,n,x; bool check(int x) //筛质数 { for (int j=2;j*j<=x;j++) if (!(x%j)) return false; return true; } int main() { for (int i=1;i<=N;i++) //预处理 { a[i]=a[i-1]; if (check(i*3+7)) a[i]++; } scanf("%d",&t); while (t--) { scanf("%d",&x); printf("%d\n",a[x]); } return 0; }