数论·梅森素数与完全数

    • 梅森数:$M_p=2^p-1$, $p$为素数。
    • 性质:若$M_p$为素数,则$p$为素数。
    • 完全数:若一个数$N$等与其所有非自身的约数之和,那么这个数就是完全数。
    • 性质:若$M_p$为素数,那么$M_p2^{p-1}=frac{M_p(M_p+1)}{2}$为完全数。
    • 性质:一个数的约数之和为$2$的幂次当且仅当它能被分解为不同梅森素数的乘积。

相关习题:

LA2995 Vivian's Probelm

代码如下:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <iostream>
#include <assert.h>
#define PI acos(-1.)
#pragma comment(linker, "/STACK:102400000,102400000")
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mp make_pair
#define st first
#define nd second
#define keyn (root->ch[1]->ch[0])
#define lson (u << 1)
#define rson (u << 1 | 1)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define type(x) __typeof(x.begin())
#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define ROF(i, t, s) for(int i = (t); i >= (s); i--)
#define dbg(x) cout << x << endl
#define dbg2(x, y) cout << x << " " << y << endl
#define clr(x, i) memset(x, (i), sizeof(x))
#define maximize(x, y) x = max((x), (y))
#define minimize(x, y) x = min((x), (y))
using namespace std;
typedef long long ll;
const int int_inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const int INT_INF = (int)((1ll << 31) - 1);
const double double_inf = 1e30;
const double eps = 1e-14;
typedef unsigned long long ul;
inline int readint(){
    int x;
    scanf("%d", &x);
    return x;
}
inline int readstr(char *s){
    scanf("%s", s);
    return strlen(s);
}
//Here goes 2d geometry templates
struct Point{
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B){
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Point A, Point B){
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p){
    return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p){
    return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b){
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x){
    if(abs(x) < eps) return 0;
    return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b){
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B){
    return A.x * B.x + A.y * B.y;
}
double Len(Vector A){
    return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B){
    return acos(Dot(A, B) / Len(A) / Len(B));
}
double Cross(Vector A, Vector B){
    return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C){
    return Cross(B - A, C - A);
}
Vector Rotate(Vector A, double rad){
    //rotate counterclockwise
    return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){
    double L = Len(A);
    return Vector(-A.y / L, A.x / L);
}
void Normallize(Vector &A){
    double L = Len(A);
    A.x /= L, A.y /= L;
}
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
    Vector u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}
double DistanceToLine(Point P, Point A, Point B){
    Vector v1 = B - A, v2 = P - A;
    return abs(Cross(v1, v2)) / Len(v1);
}
double DistanceToSegment(Point P, Point A, Point B){
    if(A == B) return Len(P - A);
    Vector v1 = B - A, v2 = P - A, v3 = P - B;
    if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
    else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
    else return abs(Cross(v1, v2)) / Len(v1);
}
Point GetLineProjection(Point P, Point A, Point B){
    Vector v = B - A;
    return A + v * (Dot(v, P - A) / Dot(v, v));
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
    //Line1:(a1, a2) Line2:(b1,b2)
    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
           c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool OnSegment(Point p, Point a1, Point a2){
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
}
Vector GetBisector(Vector v, Vector w){
    Normallize(v), Normallize(w);
    return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
}

bool OnLine(Point p, Point a1, Point a2){
    Vector v1 = p - a1, v2 = a2 - a1;
    double tem = Cross(v1, v2);
    return dcmp(tem) == 0;
}
struct Line{
    Point p;
    Vector v;
    Point point(double t){
        return Point(p.x + t * v.x, p.y + t * v.y);
    }
    Line(Point p, Vector v) : p(p), v(v) {}
};
struct Circle{
    Point c;
    double r;
    Circle(Point c, double r) : c(c), r(r) {}
    Circle(int x, int y, int _r){
        c = Point(x, y);
        r = _r;
    }
    Point point(double a){
        return Point(c.x + cos(a) * r, c.y + sin(a) * r);
    }
};
int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
    double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
    double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
    double delta = f * f - 4 * e * g;
    if(dcmp(delta) < 0) return 0;
    if(dcmp(delta) == 0){
        t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
        return 1;
    }
    t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
    t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
    return 2;
}
double angle(Vector v){
    return atan2(v.y, v.x);
    //(-pi, pi]
}
int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
    double d = Len(C1.c - C2.c);
    if(dcmp(d) == 0){
        if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
        return 0; //two circles share identical center
    }
    if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
    if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
    double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
    double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
    Point p1 = C1.point(a - da), p2 = C1.point(a + da);
    sol.pb(p1);
    if(p1 == p2) return 1;
    sol.pb(p2);
    return 2;
}
int GetPointCircleTangents(Point p, Circle C, Vector* v){
    Vector u = C.c - p;
    double dist = Len(u);
    if(dist < C.r) return 0;//p is inside the circle, no tangents
    else if(dcmp(dist - C.r) == 0){
        // p is on the circles, one tangent only
        v[0] = Rotate(u, PI / 2);
        return 1;
    }else{
        double ang = asin(C.r / dist);
        v[0] = Rotate(u, -ang);
        v[1] = Rotate(u, +ang);
        return 2;
    }
}
int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
    //a[i] store point of tangency on Circle A of tangent i
    //b[i] store point of tangency on Circle B of tangent i
    //six conditions is in consideration
    int cnt = 0;
    if(A.r < B.r) { swap(A, B); swap(a, b); }
    int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
    int rdiff = A.r - B.r;
    int rsum = A.r + B.r;
    if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
    double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
    if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
    if(d2 == rdiff * rdiff){ // internal tangency
        a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
        return 1;
    }
    double ang = acos((A.r - B.r) / sqrt(d2));
    a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
    a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
    if(d2 == rsum * rsum){
        //one internal tangent
        a[cnt] = A.point(base);
        b[cnt++] = B.point(base + PI);
    }else if(d2 > rsum * rsum){
        //two internal tangents
        double ang = acos((A.r + B.r) / sqrt(d2));
        a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
        a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
    }
    return cnt;
}
Point ReadPoint(){
    double x, y;
    scanf("%lf%lf", &x, &y);
    return Point(x, y);
}
Circle ReadCircle(){
    double x, y, r;
    scanf("%lf%lf%lf", &x, &y, &r);
    return Circle(x, y, r);
}
//Here goes 3d geometry templates
struct Point3{
    double x, y, z;
    Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
};
typedef Point3 Vector3;
Vector3 operator + (Vector3 A, Vector3 B){
    return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
}
Vector3 operator - (Vector3 A, Vector3 B){
    return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
}
Vector3 operator * (Vector3 A, double p){
    return Vector3(A.x * p, A.y * p, A.z * p);
}
Vector3 operator / (Vector3 A, double p){
    return Vector3(A.x / p, A.y / p, A.z / p);
}
double Dot3(Vector3 A, Vector3 B){
    return A.x * B.x + A.y * B.y + A.z * B.z;
}
double Len3(Vector3 A){
    return sqrt(Dot3(A, A));
}
double Angle3(Vector3 A, Vector3 B){
    return acos(Dot3(A, B) / Len3(A) / Len3(B));
}
double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
    return abs(Dot3(p - p0, n));
}
Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
    return p - n * Dot3(p - p0, n);
}
Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
    Vector3 v = p2 - p1;
    double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
    return p1 + v * t;//if t in range [0, 1], intersection on segment
}
Vector3 Cross(Vector3 A, Vector3 B){
    return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
}
double Area3(Point3 A, Point3 B, Point3 C){
    return Len3(Cross(B - A, C - A));
}
class cmpt{
public:
    bool operator () (const int &x, const int &y) const{
        return x > y;
    }
};

int Rand(int x, int o){
    //if o set, return [1, x], else return [0, x - 1]
    if(!x) return 0;
    int tem = (int)((double)rand() / RAND_MAX * x) % x;
    return o ? tem + 1 : tem;
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void data_gen(){
    srand(time(0));
    freopen("in.txt", "w", stdout);
    int times = 100;
    printf("%d
", times);
    while(times--){
        int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1);
        int b = Rand(r, 1), d = Rand(r, 1);
        int m = Rand(100, 1), n = Rand(m, 1);
        printf("%d %d %d %d %d %d %d
", n, m, a, b, c, d, r);
    }
}

struct cmpx{
    bool operator () (int x, int y) { return x > y; }
};
int debug = 1;
int dx[] = {0, 0, 1, 1};
int dy[] = {1, 0, 0, 1};
//-------------------------------------------------------------------------
const int maxn = 7e4 + 10;
int prime[maxn], k;
bool vis[maxn];
int tb[] = {2, 3, 5, 7, 13, 17, 19, 31};
void init(){
    k = 0;
    prime[k++] = 2;
    clr(vis, 0);
    for(int i = 3; i < maxn; i += 2){
        if(vis[i]) continue;
        prime[k++] = i;
        for(int j = i; j < maxn; j += i) vis[j] = 1;
    }
}
ll p[110];
int pointer, k2;
ll low_bit(ll x) { return x & (-x); }

int check(ll num){
    ++num;
    ll tem = low_bit(num);
    if(tem != num) return -1;
    int ans = 0;
    while(tem) tem >>= 1, ++ans;
    FOR(i, 0, 7) if(tb[i] == ans - 1) return i;
}

int cal(int S){
    int ans = 0, cnt = 0;
    while(S){
        if(S & 1) ans += tb[cnt];
        S >>= 1, ++cnt;
    }
    return ans;
}
void factorize(ll num){
    int curS = 0;
    FOR(i, 0, k - 1){
        if(prime[i] > num) break;
        if(num % prime[i] == 0){
            int tem = check(prime[i]);
            if(tem < 0) return;
            int cnt = 0;
            while(num % prime[i] == 0) num /= prime[i], ++cnt;
            if(cnt > 1) return;
            curS |= 1 << tem;
        }
    }
    if(num != 1){
        int tem = check(num);
        if(tem < 0) return;
        curS |= 1 << tem;
    }
    p[k2++] = curS;
}
//-------------------------------------------------------------------------
int main(){
    //data_gen(); return 0;
    //C(); return 0;
    debug = 1;
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if(debug) freopen("in.txt", "r", stdin);
    //freopen("in.txt", "w", stdout);
    init();
    while(~scanf("%d", &pointer)){
        FOR(i, 1, pointer) scanf("%lld", &p[i]);
        k2 = 0;
        FOR(i, 1, pointer) factorize(p[i]);
        //dbg(k2);
        int state[2][1 << 10];
        clr(state, 0);
        state[0][0] = 1;
        int o = 0;
        FOR(i, 0, k2 - 1){
            int lim = 1 << 10;
            int no = o ^ 1;
            clr(state[no], 0);
            FOR(j, 0, lim - 1) if(state[o][j] && !(p[i] & j)){
                state[no][j | p[i]] = 1;
            }
            FOR(j, 0, lim - 1) if(state[o][j]) state[no][j] = 1;
            o = no;
        }
        int ans = 0;
        ROF(i, (1 << 10) - 1, 0) if(state[o][i]) maximize(ans, cal(i));
        if(!ans) puts("NO");
        else printf("%d
", ans);
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    return 0;
}
code:
原文地址:https://www.cnblogs.com/astoninfer/p/5733794.html