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 ...
随机推荐
- 部分和问题(dfs)
部分和问题 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使它们的和恰好为K. 输入 首先,n和k,n表示数的个数,k表示数的和.接着一行n个数.(1<=n<= ...
- leetcode题目11.盛最多水的容器(中等)
题目描述: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其 ...
- Ngrinder脚本开发各细节锦集(groovy)
Ngrinder脚本开发各细节锦集(groovy) 1.生成随机字符串(import org.apache.commons.lang.RandomStringUtils) 数字:RandomStrin ...
- PHP 分页 (分页类)
分页类 以后可直接使用 /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...
- leetcode 207课程表
class Solution { public: bool canFinish(int numCourses, vector<vector<int>>& prerequ ...
- linux读xml文件问题
由于从配置文件中读取到的路径名的最后多了个0x0A,害我折腾了半天. 提示也很奇葩: I/O warning : failed to load external entity 关键是从这个提示看不出是 ...
- ExtJs6.2环境配置
http://extjs.org.cn/node/831 注意环境变量就是把 创建的项目文件放在C盘中
- 【Linux】【一】linux 目录切换、创建目录和文件、编辑目录以及文件(txt)
以下 是在指定目录下创建文件夹目录,以及在该目录下创建txt文件进行编辑,保存. 然后删除相关文件以及目录的命令操作记录. 本操作记录中的命令简单解释: pwd 显示当前路径 ls 显示当前目录下的文 ...
- C#与Unity 数据存储
使用Json存储数据 1.创建Json文件,直接创建txt文件,将后缀名改为xx.json 2.Json文件的数据格式,中括号表示列表,即中括号下的每个逗号隔开的内容都是列表的每个元素,花括号表示对象 ...
- 导模块的细节:(跨文件导入模块 &模块的两种执行方式) | 包的概念与使用 | 包中的相对导入语法
今日内容 包: 1. 导入模块的细节 2. 包的概念与使用 3. 包中的相对导入语法 跨文件夹导入模块 1. 假设有一个文件夹a ,a 的下面有一个ma 的模块,如果a文件夹所在目录在环境变量,a文件 ...