The Digits String
2024-10-07 02:36:38
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 ;
}
最新文章
- xml节点查询
- GoF--观察者模式
- 写给自己看的Linux运维基础(一) - 系统基础
- (转)Mac OS X内核编程,MAC驱动开发资源汇总
- ural 1123
- jQuery 元素移除empty() remove()与detach()的区别?
- js 联系电话验证实现
- 卸载cloudera manager
- 自制EIGRP配置实验大全
- CF Good Bye 2018
- [NewLife.XCode]增量累加
- 【1】【leetcode-99】 恢复二叉搜索树
- Confluence 6 下载和安装 MySQL 驱动
- Redis缓存系统(一)Java-Jedis操作Redis,基本操作以及 实现对象保存
- varnish实践
- cocos2d-x与UIKit混合编程实现半透明效果
- 使用Emmet 快速生成HTML代码
- 【原创】用JQury来制作星星打分特效功能
- Linux入侵问题排查
- C语言第九节进制
热门文章
- 去掉input type=file的默认样式
- 用 dnSpy 反编译调试 .NET 程序
- LC 272. Closest Binary Search Tree Value II 【lock,hard】
- GitHub-Microsoft:DotNet3
- javascript之Location对象
- 利用Viewpager和Fragment实现UI框架的搭建实现
- 深入学习重点分析java基础---第一章:深入理解jvm(java虚拟机) 第一节 java内存模型及gc策略
- Spring mvc中@RequestMapping 基本用法
- 阶段3 3.SpringMVC&#183;_04.SpringMVC返回值类型及响应数据类型_2 响应之返回值是String类型
- 六十三:CSRF攻击与防御之系统准备之登录与转账功能