17.7.3小结、【矩阵快速幂】2017武大校赛I题:A simple math problem即nyoj2333、 bzoj2326
来源:互联网 发布:淘宝买游戏装备流程 编辑:程序博客网 时间:2024/06/07 19:11
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
样例输入
12021
样例输出
154
提示
1 ≡ 1( mod 11)
1234567891011121314151617181920 ≡ 5( mod 11)
123456789101112131415161718192021 ≡ 4( mod 11)
来源
2017武汉大学程序设计
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define N 3typedef 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 3typedef 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;}
阅读全文
0 0
- 17.7.3小结、【矩阵快速幂】2017武大校赛I题:A simple math problem即nyoj2333、 bzoj2326
- 2017 武大校赛 I: A simple math problem(矩阵快速幂)
- 2017 Wuhan University Programming Contest 现场赛I: A simple math problem(矩阵快速幂)
- 2017 Wuhan University Programming Contest 现场赛 I.A simple math problem(矩阵快速幂)
- A simple math problem 【矩阵快速幂】
- HDU - problem 1757 A Simple Math Problem【矩阵 + 快速幂】
- A Simple Math Problem (矩阵快速幂、构造矩阵)
- hdu - 1757 - A Simple Math Problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem (矩阵快速幂)
- [HDU 1757] A Simple Math Problem (矩阵快速幂)
- HDU 1757-A Simple Math Problem(矩阵快速幂)
- HDU 1757 A Simple Math Problem 矩阵快速幂
- Hdu1757 - A Simple Math Problem - 矩阵快速幂
- hdu 1757 A Simple Math Problem(矩阵快速幂)
- HDU 1757 A Simple Math Problem (矩阵快速幂)
- HDU 1757 A Simple Math Problem(矩阵快速幂)
- HDU1757-A Simple Math Problem(矩阵快速幂)
- A New General Deep Learning Approach for Natural Language Processing
- PHP大整数乘积
- Redis持久化 (17)
- 第一章安装OpenResty(Nginx+Lua)开发环境
- HDU 6027 Easy Summation (gcd)
- 17.7.3小结、【矩阵快速幂】2017武大校赛I题:A simple math problem即nyoj2333、 bzoj2326
- leetcode 56. Merge Intervals
- redisson client 介绍及优缺点 (18)
- 为什么大多数人的Facebook 付费广告都在亏钱?
- 3211: 花神游历各国
- Redis事务
- 利用递归求n的阶乘
- 第二章 OpenResty(Nginx+Lua)开发入门
- 架构师之路--视频业务介绍,离线服务架构和各种集群原理(1/2)