HDU 1452 Happy 2004 factor and __ Number theory *

Source: Internet
Author: User

Ask: Input x, 2004^x all the factors of the and, the result of 29.

Solution: Must not be violent, need to think of other methods, we know is: a number of X=P1^T1*P2^T2*...*PK^TK (of which PI is prime). The number of factors is (t1+1) * (t2+1) *...* (tk+1). How did you get that? Enumerating each exponent 0~ti, we can obtain all the factors. So factor and sum=p1^0*p2^0*...*pk^0+p1^0*p2^0*...*pk^1+......+p1^t1*p2^t2*. pk^tk= (P1^0+P1^1...+PK^T1) *...* (PK^0+PK^1+...+PK^TK). This is the product of the sum of the geometric progression. The next step is simple, 2004=2^2*3*167, so 2004^x=2^2x*3^x*167^x, Factor and sum= (2^0+...+2^2x) * (3^0+ ...). 3^X) * (167^0+...+167^x) = (2^ (2x+1)-1) * (3^ (x+1)-1)/2) * (167^ (x+1)-1)/166). One by one can be taken.

Here the division has been used before, two methods:

1. (A/b)%mod=a% (b*mod)/b%mod;

2. (A/b)%mod=a*b^ (mod-2)%mod,mod is prime (can be proved by the inverse)



Time Consuming: 0ms/1000ms

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int mod=29;
int pow_mod (int a,int b)
{
	int s=1;
	while (b)
	{
		if (b&1) s= (s*a)%mod;
		A= (a*a)%mod;
		b=b>>1;
	}
	return s;
}
int find (int a,int b)
{
	int i,j,k,p,ans;
	Ans=pow_mod (a,b+1)-1;
	Ans= (Ans+mod)%mod;
	Ans=ans*pow_mod (a-1,mod-2)%mod;
	return ans;
}
int main ()
{
	int x;
	while (cin>>x)
	{
		if (x==0) break;
		Cout<<find (2,2*x) *find (3,x) *find (167,x)%mod<<endl;
	}
	return 0;
}
/* 2004=2*2*3*167;
*/



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.