The Digits String
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 ;
}
The Digits String的更多相关文章
- L The Digits String(没有写完,有空补)
链接:https://ac.nowcoder.com/acm/contest/338/L来源:牛客网 Consider digits strings with length n, how many d ...
- python中string模块
>>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL ...
- python string module
String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...
- [Swift]LeetCode949. 给定数字能组成的最大时间 | Largest Time for Given Digits
Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour t ...
- (九)random、os、time、datetime、hashlib、pymysql、excel、sys、string相关模块
1.random模块 1 import random,string 2 print(string.printable) #代表所有的 数字+字母+特殊字符 3 4 print(random.randi ...
- python 加密算法及其相关模块的学习(hashlib,random,string,math)
加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...
- 738. Monotone Increasing Digits 单调递增的最接近数字
[抄题]: Given a non-negative integer N, find the largest number that is less than or equal to N with m ...
- Python 3之str类型、string模块学习笔记
Windows 10家庭中文版,Python 3.6.4, Python 3.7官文: Text Sequence Type — str string — Common string operatio ...
- python拓展2 collections模块与string模块
知识内容 1.collections模块介绍 2.collections模块使用 3.string模块介绍及使用 一.collections模块介绍 collections模块中提供了很多python ...
随机推荐
- Android学习_7/25
常用控件 Android控件使用规律:先定义id,再指定宽度和高度,然后适当加入一些控件特有的属性 1. TextView 在界面上显示一段文本 2. Button ...
- (十七)C语言之变量
- php phpexcel 读取excel文件数据
public function readExcel(){ $allPath = '/home/examine\video/list.xls'; \think\Loader::import('exten ...
- pandas.Series.value_counts
pandas.Series.value_counts Series.value_counts(normalize=False, sort=True, ascending=False, bins=Non ...
- python 构建自己的log系统
python的logging模块提供了记录程序运行情况的日志功能,类似于Apache的log4j,很好很强大,这里我们就来看一下Python中内置的日志模块logging用法详解 logging模块简 ...
- vue问题六:表单验证
表单验证规则 查看文档:https://blog.csdn.net/weixin_42018790/article/details/80762149 1). el-form增加 :rules=&quo ...
- Linux命令之文件和目录操作命令(一)—— ls
本文介绍了ls命令的最常用法. 1.用法: ls [选项][目录名或文件名] 2.选项: -l 使用长格式显示目录或文件的详细信息 -a 显示隐藏文件 -h 人性化显示文件或目录的大小 -d 显示目录 ...
- redis5.0编译安装
##################### linux下运行redis 1.下载源码 https://redis.io/ 2.将源码上传到服务器中,进行编译安装 a)可以使用rz进行上传(sz是将服务 ...
- Java异常超详细总结
1.1,什么是异常: 异常就是Java程序在运行过程中出现的错误. 骚话: 世界上最真情的相依就是你在try我在catch,无论你发什么脾气,我都静静接受,默默处理(这个可以不记) 1.2,异常继 ...
- JS方法调用jQuery内部方法
转载于:https://blog.csdn.net/tsoTeo/article/details/77848932 已经测试过,可以正常调用!!以下为原文: JS方法能不能调用JQuery里面的方法 ...