hdu 2197 本原串

Problem Description
由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?
答案mod2008.
例如,100100不是本原串,因为他是由两个100组成,而1101是本原串。
 
Input
输入包括多个数据,每个数据一行,包括一个整数n,代表串的长度。
 
Output
对于每个测试数据,输出一行,代表有多少个符合要求本原串,答案mod2008.
 
Sample Input
1 2 3 4
 
Sample Output
2 2 6 12
 
Author
scnu
 
Recommend
lcy   |   We have carefully selected several similar problems for you:   2196  2193  2195  1798  2159 
很有意思的题目,求出所有的排列,然后减去任何可以整除它的数的情况,最后减去2,全部0或全部1就是答案。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 110000
#define mod 2008
using namespace std;
int quick_pow(int a,int b) {
    int d = 1;
    a %= mod;
    while(b) {
        if(b % 2) d = (d * a) % mod;
        a = (a * a) % mod;
        b /= 2;
    }
    return d;
}
int getans(int n) {
    if(n <= 2) return 2;
    int ans = quick_pow(2,n);
    for(int i = 2;i * i <= n;i ++) {
        if(n % i == 0) {
            ans -= getans(i);
            if(n / i != i) ans -= getans(n / i);
        }
    }
    ans -= 2;
    if(ans < 0) ans += abs(ans / 2008 - 1) * 2008;///全程取余 可能会是负数,要取正
    return ans % mod;
}
int main() {
    int n;
    while(~scanf("%d",&n)) {
        printf("%d\n",getans(n));
    }
}

猜你喜欢

转载自www.cnblogs.com/8023spz/p/9827493.html