本原串

本原串

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 580    Accepted Submission(s): 202


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
 
 
从反面思考:
代码:
 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #include <math.h>
 5 using namespace std;
 6 #define mod 2008
 7 #define ll int
 8 int a[100000001]={0};
 9 ll exp_mod(ll a,ll n)//(a^n)%b
10 {
11     ll t;
12     if(n==0)
13         return 1;
14     t=exp_mod(a,n/2);
15     t=t*t%mod;
16     if((n&1)==1)
17         t=t*a%mod;
18     return t;
19 }
20 int fun(int n)
21 {
22     int i,j;
23     if(a[n])
24     {
25         return a[n];
26     }
27     int an=exp_mod(2,n)-2;
28     for(i=2; i<=sqrt(n) ;i++)
29         if(n%i==0)
30         {
31             an-=fun(i);
32             if(i!=n/i)
33             an-=fun(n/i);
34             an%=mod;
35         }
36     return (an+mod)%mod;
37 }
38 int main()
39 {
40     int n;
41     a[1]=a[2]=2,a[3]=6,a[4]=12;
42     while(~scanf("%d",&n))
43     {
44         printf("%d\n",fun(n));
45     }
46 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3624496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值