1. 程式人生 > >hdu 2197 本原串

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)); } }