hdu 2276 矩阵 二分
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 110;
int X, mod = 2;
int hash[MAXN][MAXN];
int sum[MAXN][MAXN];
int a[MAXN][MAXN];
int result[MAXN];
int len;
int matrix_binary()
{
int n = len;
long i, j, t, p, k;
memset(sum, 0, sizeof(sum));
memset(hash, 0, sizeof(hash));
for (i = 0; i < n; i++)
{
sum[i][i] = 1;
hash[i][i] = 1;
sum[(i + n - 1) % n][i] = 1;
}
for (p = X; p > 1;)
{
if (p & 1)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
for (t = 0, k = 0; k < n; k++)
{
t += hash[i][k] * sum[k][j];
}
a[i][j] = t % mod;
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
hash[i][j] = a[i][j];
}
}
p--;
}
else
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
for (t = 0, k = 0; k < n; k++)
{
t += sum[i][k] * sum[k][j];
}
a[i][j] = t % mod;
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = a[i][j];
}
}
p = p >> 1;
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
for (t = 0, k = 0; k < n; k++)
{
t += sum[i][k] * hash[k][j];
}
a[i][j] = t % mod;
}
}
return 0;
}
int main()
{
char c[110];
while (scanf("%d", &X) != EOF)
{
scanf("%s", c);
len = strlen(c);
int i, j;
for (i = 0; i < len; ++i)
{
if (c[i] == '0')
{
result[i] = 0;
}
else
{
result[i] = 1;
}
}
matrix_binary();
//for (i = 0; i < len; ++i)
//{
// for (j = 0; j < len; ++j)
// {
// printf("%d ", a[i][j]);
// }
// puts("");
//}
int tmp[MAXN];
for (i = 0; i < len; ++i)
{
int t = 0;
for (j = 0; j < len; ++j)
{
t += result[j]*a[j][i];
}
tmp[i] = t % mod;
}
for (i = 0; i < len; ++i)
{
printf("%d", tmp[i]);
}
puts("");
}
return 0;
}