HDU Primitive string (fast power)

Source: Internet
Author: User
Tags printf
problem DescriptionA string consisting of 0 and 1 cannot be represented as a string connected by several identical smaller strings, called primitive strings, and how many primitive strings are of n (n<=100000000) long.
The answer mod2008.
For example, 100100 is not an primitive string, because he is composed of two 100, and 1101 is the primitive string.
InputThe input includes multiple data, one row per data, and an integer n, which represents the length of the string. OutputFor each test data, the output line, representing how many meet the requirements of the primitive string, the answer mod2008.
Sample Input
1
2
3
4
Sample Output
2
2
6
12
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<int, int >mp;
const int mod=2008;

int powmod (int a,int n)
{
	if (n==0) return 1;
	int X=powmod (A,N/2)%mod;
	Long Long ans= (long Long) x*x%mod;
	if (n%2==1) ans=ans*a%mod;
	return (int) ans;

The key is to find the approximate n ... 
int sol (int n)
{
	if (mp[n]!=0) return mp[n];
	Mp[n]=powmod (2,n)-2;
	for (int i=2;i*i<=n;i++) {//  why not I<=N/2, in order to prevent timeouts, approximate (similar to the method of prime number)
		                      //y=n*m, effective reduction, is optimized. 
		if (n%i==0) {
			mp[n]= (Mp[n]-sol (i) +mod)%mod;
			if (n/i!=i) mp[n]= (Mp[n]-sol (n/i) +mod)%mod;
		}
		
	}
	return mp[n];
}

int main ()
{
	int n;
	while (scanf ("%d", &n)!=eof) {
	   mp[0]=0;
       mp[1]=2;
       mp[2]=2;
       if (n==0) {
       	 printf ("0\n");
       	 Continue;
	   }
	   printf ("%d\n", Sol (n));
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.