HDU 2197 - 本原串 (思维)

思路

$S(n) = 2^n - \sum S(i) - 2$,i是n的因子。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e6 + 10;
const int MOD = 2008;
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
int cases = 0;
typedef pair<int, int> pii;
 
LL pow_mod(LL a, LL m, LL n)
{
    LL ret = 1;
    while (m)
    {
        if (m & 1) ret = ret * a % n;
        a = a*a % n;
        m >>= 1;
    }
    return ret;
}
map<int, int> mp;
 
int Solve(int n)
{
    if (mp.count(n)) return mp[n];
    mp[n] = pow_mod(2, n, MOD) - 2;
    for (int i = 2; i <= (int)sqrt(n+0.5); i++)
    {
        if (n % i == 0)
        {
            mp[n] = (mp[n] - Solve(i) + MOD) % MOD;
            if (n / i != i) mp[n] = (mp[n] - Solve(n/i) + MOD) % MOD;
        }
    }
    return mp[n];
}
 
int main()
{
    int n;
    mp[1] = 2; mp[2] = 2; mp[0] = 0;
    while (~scanf("%d", &n))
    {
        if (mp.count(n) || n <= 2) printf("%d\n", mp[n]);
        else printf("%d\n", Solve(n));
    }
    return 0;
}

Powered by Jekyll and Theme by solid