快速幂1007

来源:互联网 发布:陈巧生香炉官网淘宝网 编辑:程序博客网 时间:2024/06/03 17:19

本原串

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 15

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?
答案mod2008.
例如,100100不是本原串,因为他是由两个100组成,而1101是本原串。

Input

输入包括多个数据,每个数据一行,包括一个整数n,代表串的长度。

Output

对于每个测试数据,输出一行,代表有多少个符合要求本原串,答案mod2008.

Sample Input

1234

Sample Output

226

12

思路:可以用0和1的总串减去非本原子串,而非本原子串是由n的因子组成。

代码:

#include <stdio.h>#include <algorithm>#include <iostream>#include <iostream>#include <string>#include <map>#include <cmath>using namespace std;const int N=2008;map<int,int>mp;long long int n;int poww(int a,int b){   long long int m=1,n=a%N;    while(b)    {        if(b&1)m=m*n%N;        n=n*n%N;        b>>=1;    }    return m;}int ff(long long int x){    if(mp[x]!=0)return mp[x];    if(x==1) return mp[x]=2;   long long int ans=poww(2,x);    ans=(ans-2)%N;    for(int i=2;i*i<=x;i++)    {        if(x%i!=0)continue;        if(i*i==x)        {            ans=(ans-ff(i))%N;            continue;        }        else        {            ans=(ans-ff(i))%N;            ans=(ans-ff(x/i))%N;        }    }    return mp[x]=(ans+N)%N;}int main(){    while(scanf("%d",&n)!=EOF)    {        printf("%d\n",ff(n));    }    return 0;}