HDU - 6156 Palindrome Function
This problem asks us to calculate the total number of several specific numbers in different binary systems which is a palindrome number. However, the final result is not just the sum, That is to say,according to the contents of the problem, we need to calculate this formula: (
)as our output contents. In the sample input, the problem gives 3 test cases, the first of which suggests the result of the function of number "1" from 2 to 36 of binary system, and the followings are of the same patterns(hence we no longer elaborate the essentials).
After comprehending the problem, we have to analyze the details. First, make a change of the format of the function:. Then,
, hence the result could be changed into the format of
. As a result, we have to calculate F(N, d) as our final result. As we can see, the number "n" in binary system "d" can be expressed as
. Then our aim is to find the total number of the palindrome numbers from 1 to N in the binary system "d". It is easy to get that the maximum number in the D base is (n+1). So if i in D system has only 1 bit (if n>=1), then i must meet i < = N, in the range of the number of [1, D - 1] in the current bits. Consider the i in D system has 2 bits, if n>=2, as long as meet i0=i1(right now i must meet i<= N), and the number can also be D - 1. Considering the i has several bits (3<=bit<=n) in D system, when i meets i<= N, and the range of
is [1, d-1], and in the middle of the (bit-2) bits, the palindromes equal,which means
. Finally, if i in the d base has (1 + n) bits, there are 2 different kinds of situations, if "in"<Nn, "in" has (Nn-1) kinds of values and "in"==i0. The middle palindrome values equals means
. Then, if "in"==Nn, we just have to consider the middle bit, or we have to also include the previous bit.
Here, I'd be glad to share a correct code with you:
#include <stdio.h>
typedef long long ll;
ll L, R, l, r,a[35];
ll qpow(ll a, ll b)
{
ll res = 1;
while(b)
{
if(b & 1)
res *= a;
a *= a;
b >>= 1;
}
return res;
}
ll Div(ll x)
{
return x % 2 == 0 ? x / 2 : x / 2 + 1;
}
ll Cal(ll d, ll bit)
{
return 2 * (qpow(d, bit / 2 + 1) - d) / (d - 1) + (bit & 1 ? qpow(d, (bit + 1) /2) : 0);
}
ll dfs(ll a[], ll cnt, ll d)
{
if(cnt < 0)
return 1;
if(cnt == 0)
return a[cnt] + 1;
ll num = 0;
num += a[cnt] * (cnt == 1 ? 1 : qpow(d, (ll)Div(cnt - 1)));
if(a[cnt] > a[0])
{
--a[1];
ll id = 1;
while(a[id] < 0 && id < cnt)
{
a[id] += d;
--a[++id];
}
if(id == cnt)
return num;
}
return num + dfs(a + 1, cnt - 2, d);
}
ll cal(ll x, ll d)
{
ll cnt = 0;
ll tmp = x;
while(tmp)
{
a[cnt++] = tmp % d;
tmp /= d;
}
cnt = cnt - 1;
if(cnt < 0)
return 0;
if(cnt == 0)
return a[cnt];
ll num = 0;
num += (a[cnt] - 1) * (cnt == 1 ? 1 : qpow(d, Div(cnt - 1))) + (cnt > 2 ? (d - 1) * Cal(d, cnt - 2) : 0)+ (d - 1) * (cnt >= 2?2:1);
if(a[cnt] > a[0])
{
--a[1];
ll id = 1;
while(a[id] < 0 && id < cnt)
{
a[id] += d;
--a[++id];
}
if(id == cnt)
return num;
}
return num + dfs(a + 1, cnt - 2, d);
}
int main()
{
int T, kas = 0;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld%lld%lld", &L, &R, &l, &r);
ll ans = 0;
for(ll i = l; i <= r; ++i)
ans += (ll)(R - L + 1) + (i - 1) * (cal(R, i) - cal(L - 1, i));
printf("Case #%d: %lld\n", ++kas, ans);
}
return 0;
}
F(N,d)(1<=N<=1e9,2<=d<=36),which is the final result.