本原串

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

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
 
F[n]=2^n-F[k],k为n的约数。
此题用反面情况求解,长度为n的串有2^n中,减去非本原串,非本原串肯定是由长度为v字串不断重复u次得到的,那么v必然是n的约数。
感觉很长时间没做题了脑袋笨了不少,约数枚举法忘了。不然肯定超时
 #include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
map<int,int> m;
map<int,int>::iterator ite;
LL n,ans;
LL mod_pow(LL x,LL n,int mod)
{
LL res=;
while(n)
{
if(n&)
res=res*x%mod;
x=x*x%mod;
n>>=;
}
return res;
}
int cal(LL n)
{
int i,j;
if(m[n]!=)
return m[n];
m[n]=mod_pow(,n,)-;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
m[n]=(m[n]-cal(i)+)%; //不然可能是负数
if(i*i!=n)
m[n]=(m[n]-cal(n/i)+)%; }
}
return m[n];
}
int main()
{
m[]=;
m[]=;
m[]=;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
if(n<=)
printf("%d\n",m[n]);
else
{
m[n]=cal(n);
printf("%d\n",m[n]);
}
}
}
 

本原串(HDU 2197 快速幂)的更多相关文章

  1. 本原串(hdu 2197)

    本原串 题目链接 思路: 反向想将总的个数减去不符合要求的个数.我们枚举n的约数,然后把n平均分,就可以构成不符合要求的串,\(g[i]\)表示循环节长为i约数的个数\(2^i\),我们要求循环节为\ ...

  2. hdu 1061 快速幂

    求n^n的个位 Sample Input 2 3 4 Sample Output 7 6 直接快速幂了,注意要用long long #include<cstdio> long long q ...

  3. C - Rikka with Badminton --- HDU 6425 快速幂加组合数学

    ---恢复内容开始--- 题目链接: https://vjudge.net/problem/1812693/origin 这题的mod运算很恶心,真的... 本题有正反两个思路,一个是正面求解其不能成 ...

  4. hdu 5187 快速幂 + 快速乘 值得学习

    就是以那个ai为分水岭,左边和右边都分别是单调增或单调减如图         就这四种情况,其中头两种总共就是两个序列,也就是从头到尾递增和从头到尾递减.         后两种方式就是把序列中德数分 ...

  5. hdu 1852(快速幂模+有除法的时候取模的公式)

    Beijing 2008 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Tota ...

  6. hdu 4506 快速幂

    小明自从告别了ACM/ICPC之后,就开始潜心研究数学问题了,一则可以为接下来的考研做准备,再者可以借此机会帮助一些同学,尤其是漂亮的师妹.这不,班里唯一的女生又拿一道数学题来请教小明,小明当然很高兴 ...

  7. HDU 1097 快速幂

    #include<iostream> using namespace std; long long quick(long long a,long long b,int c) { ; a=a ...

  8. hdu 2197 求长度为n的本原串 (快速幂+map)

    Problem Description由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,10 ...

  9. 数学--数论--HDU 2197 本原串 (推规律)

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

随机推荐

  1. Windows10关闭快速启动

  2. PHP常用字符串函数

    1:访问子字符串:strpos()2:提取子字符串:substr()3:逐字节处理字符串:strstr()4:计算字符串长度:strlen()5:替换子字符串:substr_replace()6:按字 ...

  3. Practice: Process logs with Apache Hadoop

    http://www.ibm.com/developerworks/library/os-log-process-hadoop/ Analyzing Apache logs with Apache P ...

  4. 系统共享内存的修改(ORA-27102: out of memory)

    http://ccchencheng.blog.51cto.com/2419062/738188 http://blog.csdn.net/leshami/article/details/874602 ...

  5. 迅雷程浩:企业外包服务,下一个大的风口?(2B业务一定要懂销售和营销的人,这点和2C 不一样)

    我今年暑假去了趟硅谷,一天去一个朋友的公司拜访,发现这公司没有前台,前台桌子上放了一个显示器.我刚进去,显示器里的老印就跟我打招呼 "How may I help you?" 事后 ...

  6. android实现对导航Tab设置下划线选中效果

    技术人员核心竞争力还是技术啊.努力提高各种实现效果.加油哦! 直接看效果.此linearLayout只有两个Button ,当选中Button1,Button1有个下划线选中效果.当选中Buton2, ...

  7. HDU 5505 - BestCoder Round #60 - GT and numbers

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...

  8. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  9. muduo源代码分析--我对muduo的理解

    分为几个模块 EventLoop.TcpServer.Acceptor.TcpConnection.Channel等 对于EventLoop来说: 他仅仅关注里面的主驱动力,EventLoop中仅仅关 ...

  10. IOS成长之路-Nsstring中搜索方法rangeOfString

    NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1这个字符 ...