17.7.3暑假集训小结:
今天早上踩着8点进来了,昨天刚从家里到学校,先收拾了一下电脑到九点半;
从今天开始的计划是上学期考试的时候没刷的题目刷一下,到7.5;
一上午也没有做出这道题目,一直在学习有关这道题目的博客,慢慢的也就理解了;
也知道是个矩阵快速幂,但是切入点不对,说白了不就是需要知道定义的n值然后取余11就好了!
所以就用矩阵快速幂边求得定义的n值边取余啦~;然后下午給A了,下午学习的是容斥定理;
2333: A simple math problem
时间限制: 1 Sec 内存限制: 512 MB提交: 38 解决: 14
[ 提交][ 状态][ 讨论版]
题目描述
Given a number n, you should calculate 123456 . . . 11121314 . . . n module 11.
输入
A single line with an integer n (0 < n ≤ 10e18) 单组数据测试.
输出
Output one integer, 123456 . . . 11121314 . . . n module 11
样例输入
1
20
21
样例输出
1
5
4
提示
1 ≡ 1( mod 11)
1234567891011121314151617181920 ≡ 5( mod 11)
123456789101112131415161718192021 ≡ 4( mod 11)
来源
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 3
typedef long long LL;
LL n,m=11;
struct Matrix
{
LL mat[N][N];
Matrix()
{
memset(mat,0,sizeof(mat));
}
};
Matrix mul(Matrix a,Matrix b)
{
Matrix res;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
for(int k=0; k<N; k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
res.mat[i][j]%=m;
}
}
return res;
}
Matrix pow_matrix(LL n,LL t)
{
Matrix res;
for(int i=0; i<N; i++)
res.mat[i][i]=1;
Matrix b; //每次矩阵快速幂前对矩阵进行改变,根据数据范围最多快速幂18次;
b.mat[0][0]=n%m;
b.mat[0][1]=b.mat[0][2]=b.mat[1][1]=b.mat[1][2]=b.mat[2][2]=1;
LL y=t+1-n/10;
while(y)
{
if(y&1)
res=mul(res,b);
y>>=1;
b=mul(b,b);
}
return res;
} //模板上的输入变量是:矩阵、数据长度;灵活运用模板,依题目而变(说着轻松)
int main()
{
while(~scanf("%lld",&n))
{
Matrix ans;
ans.mat[2][0]=1;
int x[9]= {0,1,1,2,2,3,3,3,4};
if(n<9)
{
printf("%d\n",x[n]);
continue;
}
LL t=10;
for(int i=1; i<=18; i++)
{
ans=mul(pow_matrix(t,t-1),ans);
t*=10;
if(t>n) break;
}
ans=mul(pow_matrix(t,n),ans);
printf("%lld\n",ans.mat[0][0]);
}
return 0;
}
2326: [HNOI2011]数学作业
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2180 Solved: 1269
[ Submit][ Status][ Discuss]
Description
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 3
typedef long long LL;
LL n,m;
struct Matrix
{
LL mat[N][N];
Matrix()
{
memset(mat,0,sizeof(mat));
}
};
Matrix mul(Matrix a,Matrix b)
{
Matrix res;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
for(int k=0; k<N; k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
res.mat[i][j]%=m;
}
}
return res;
}
Matrix pow_matrix(LL n,LL t)
{
Matrix res;
for(int i=0; i<N; i++)
res.mat[i][i]=1;
Matrix b;
b.mat[0][0]=n%m;
b.mat[0][1]=b.mat[0][2]=b.mat[1][1]=b.mat[1][2]=b.mat[2][2]=1;
LL y=t+1-n/10;
while(y)
{
if(y&1)
res=mul(res,b);
y>>=1;
b=mul(b,b);
}
return res;
}
int main()
{
while(~scanf("%lld",&n))
{
Matrix ans;
// for(int i=0; i<N; i++)
// ans.mat[i][i]=1;
ans.mat[2][0]=1;
// int x[9]= {0,1,1,2,2,3,3,3,4};
// if(n<9)
// {
// printf("%d\n",x[n]);
// continue;
// }
scanf("%lld",&m);
LL t=10;
for(int i=1; i<=18; i++)
{
ans=mul(pow_matrix(t,t-1),ans);
t*=10;
if(t>n) break;
}
ans=mul(pow_matrix(t,n),ans);
printf("%lld\n",ans.mat[0][0]);
}
return 0;
}