码迷,mamicode.com
首页 > 其他好文 > 详细

[UVA1434] YAPTCHA(数论,威尔逊定理)

时间:2016-08-31 00:48:28      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hust.edu.cn/vjudge/problem/36250

题意:求那个式子。

设3k+7=x,则化简成 Sn=Σ(k=1~n) (((x-1)!+1/x)-[(x-1)!/x])

根据威尔逊定理,假如一个数p是素数,则这个数满足:(p-1)!=-1 (mod p)即 (p-1)!-1=0(mod p)。

由于被减数满足此条件,而减数表示向下取整。则被减数整除,减数一定是向下取整的。所以结果减数比被减数要小1,否则减数和被减数相等,即为0。问题转换成了求3k+7是否是素数,打表即可。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &(a))
 43 #define Rll(a) scanf("%lld", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f, sizeof(a))
 53 #define lrt rt << 1
 54 #define rrt rt << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long Uint;
 62 typedef pair<int, int> pii;
 63 typedef pair<LL, LL> pLL;
 64 typedef pair<string, LL> psi;
 65 typedef map<string, LL> msi;
 66 typedef vector<LL> vi;
 67 typedef vector<LL> vl;
 68 typedef vector<vl> vvl;
 69 typedef vector<bool> vb;
 70 
 71 const int maxn = 6000100;
 72 bool isprime[maxn];
 73 int n;
 74 int dp[maxn/6];
 75 
 76 void init() {
 77     int nn = int(sqrt(maxn));
 78     memset(isprime, true, sizeof(isprime));
 79     isprime[0] = isprime[1] = 0;
 80     for(int i = 2; i <= nn; i++) {
 81         if(isprime[i]) {
 82             int p = maxn / i;
 83             for(int j = 2; j <= p; j++) {
 84                 isprime[j*i] = 0;
 85             }
 86         }
 87     }
 88 }
 89 
 90 int main() {
 91     // FRead();
 92     init();
 93     Cls(dp); dp[1] = 0;
 94     For(i, 2, 1000010) {
 95         dp[i] = dp[i-1];
 96         if(isprime[3*i+7]) dp[i]++;
 97     }
 98     int T;
 99     Rint(T);
100     W(T) {
101         Rint(n);
102         cout << dp[n] << endl;
103     }
104     RT 0;
105 }

 

[UVA1434] YAPTCHA(数论,威尔逊定理)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5824106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!