https://ac.nowcoder.com/acm/contest/338/L

题解:

当n==1时,0-9填上的话,对4取余,分别是余数为0的3个,1的3个,2的2个,3的2个;

当n==2时,因为一个数的时候有3323的余数个数分布,如果第2个填上数可以使原来的余数变成0或者保持零,那么可以填上;

当n>=3时,还是根据前面的余数分布决定接下来可以填什么数。

暴力递推

#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=+;
const int MOD=;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,q,ans;
ll a[];
char str;
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
while(~scanf("%lld",&n)){
memset(a,,sizeof(a));
a[]=;
a[]=;
a[]=;
a[]=;
for(int i=;i<=n;i++){
int x=a[],y=a[],z=a[],t=a[];
a[]=(x*+y*+z*+t*)%MOD;
a[]=(x*+y*+z*+t*)%MOD;
a[]=(x*+y*+z*+t*)%MOD;
a[]=(x*+y*+z*+t*)%MOD;
}
cout<<a[]<<endl;
}
//cout << "Hello world!" << endl;
return ;
}

很明显肯定会超时。

a数组和递推a数组的系数可以构成同一个方矩阵,那么可以通过矩阵快速幂减少时间复杂度。所求的答案就是方阵a的n-1次幂中的a[]0[0];

矩阵快速幂

#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=;
const int MOD=;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,q,ans;
ll a[][]={,,,,,,,,,,,,,,,};
ll b[][]={,,,,,,,,,,,,,,,};
int tmp[N][N];
void multi(ll a[][],ll b[][])
{
memset(tmp,,sizeof tmp);
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j])%MOD;
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=tmp[i][j];
}
char str;
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
while(~scanf("%lld",&n)){
ll a[][]={,,,,,,,,,,,,,,,};
ll b[][]={,,,,,,,,,,,,,,,};
n--;
while(n){
if(n&){
multi(a,b);
}
multi(b,b);
n/=;
}
cout<<a[][]<<endl;
}
//cout << "Hello world!" << endl;
return ;
}

最新文章

  1. xml节点查询
  2. GoF--观察者模式
  3. 写给自己看的Linux运维基础(一) - 系统基础
  4. (转)Mac OS X内核编程,MAC驱动开发资源汇总
  5. ural 1123
  6. jQuery 元素移除empty() remove()与detach()的区别?
  7. js 联系电话验证实现
  8. 卸载cloudera manager
  9. 自制EIGRP配置实验大全
  10. CF Good Bye 2018
  11. [NewLife.XCode]增量累加
  12. 【1】【leetcode-99】 恢复二叉搜索树
  13. Confluence 6 下载和安装 MySQL 驱动
  14. Redis缓存系统(一)Java-Jedis操作Redis,基本操作以及 实现对象保存
  15. varnish实践
  16. cocos2d-x与UIKit混合编程实现半透明效果
  17. 使用Emmet 快速生成HTML代码
  18. 【原创】用JQury来制作星星打分特效功能
  19. Linux入侵问题排查
  20. C语言第九节进制

热门文章

  1. 去掉input type=file的默认样式
  2. 用 dnSpy 反编译调试 .NET 程序
  3. LC 272. Closest Binary Search Tree Value II 【lock,hard】
  4. GitHub-Microsoft:DotNet3
  5. javascript之Location对象
  6. 利用Viewpager和Fragment实现UI框架的搭建实现
  7. 深入学习重点分析java基础---第一章:深入理解jvm(java虚拟机) 第一节 java内存模型及gc策略
  8. Spring mvc中@RequestMapping 基本用法
  9. 阶段3 3.SpringMVC&#183;_04.SpringMVC返回值类型及响应数据类型_2 响应之返回值是String类型
  10. 六十三:CSRF攻击与防御之系统准备之登录与转账功能