For the 2004^x factor and modulo 29, sum (2004^X) represents the 2004^x factor and sum (2004^x) =sum (4^x*3^x*167^x), sum () is a integrable function,
Then there is sum (2004^x)%29=sum (4^x) *sum (3^x) *sum (167^x)%29
=sum (2^ (2*x)) *sum (3^x) *sum (167^x)%29=sum (2^ (2*x)) *sum (3^x) *sum (22^x)%29
Sum (p^ (X)) =1+p+p^2+......+p^n= (p^ (n+1)-1)/(P-1)
Then there are: Sum (2^ (2*x)) *sum (3^x) *sum (22^x)%29= (2^ (2*x)-1) * (3^ (x+1)-1)/2* (22^ (x+1)-1)/21)%29
Also: 15*2%29=1,21*18%29=1, then there are 15 2 modulo 29 of the multiplicative inverse, 18 is 21 modulo 29 of the multiplicative inverse
So you can get:
SUM (2004^x)%29= (2^ (2*x)-1) * (3^ (x+1)-1) *15* (22^ (x+1)-1) *18)%29
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
# Include<vector>
#define MM (a,b) memset (A,b,sizeof (a))
using namespace std;
const int INF=0X7FFFFFF;
Const double Pi=acos ( -1.0);
const double eps=1e-8;
const double e=2.7182818284590452354;
Computes the A^BMODN
int modexp (int a,int b,int N)
{
int ret=1;
while (b)
{
if (b&1) ret=ret*a%n;
A= (a*a)%n;
b>>=1;
}
return ret;
}
int main ()
{
int n;
while (~SCANF ("%d", &n), N)
{
int ans1=modexp (2,2*n+1,29);
int Ans2=modexp (3,n+1,29);
int Ans3=modexp (22,n+1,29);
int ans= ((ans1-1) * (ans2-1) * (ans3-1) *15*18)%29;
printf ("%d\n", ans);
}
return 0;
}