`

Ural 1009. K-based Numbers

阅读更多
1009. K-based Numbers

Time Limit: 1.0 second
Memory Limit: 16 MB

Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn’t contain two successive zeros. For example:
1010230 is a valid 7-digit number;
1000198 is not a valid number;
0001235 is not a 7-digit number, it is a 4-digit number.
Given two numbers N and K, you are to calculate an amount of valid K based numbers, containing N digits.
You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 18.
Input
The numbers N and K in decimal notation separated by the line break.
Output
The result in decimal notation.
Sample
input output
2
10

90

题目大意:给定一个正整数k和n( 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 18),试确定K进制的N位数且没有连续的两个0及没有前导0。

算法分析:此题是Fibonacci数列的变形题。设f(n)表示符合题目条件的n位K进制的数的总数。则有:f(n)=(k-1)*(f(n-1)+f(n-2)),且f(1)=k-1,f(2)=k*(k-1)

注意:当n==1时,直接输出k,进行特判!



#include <iostream>
using namespace std;
typedef long long lld;
const int maxn=20;
int main()
{
    lld f[maxn];
    lld n,k;
    cin>>n>>k;
    if(n==1)    {cout<<k<<endl;return 0;}//特判n=1的情况
    f[1]=k-1;
    f[2]=k*(k-1);
    for(lld i=3;i<=n;i++)
    {
        f[i]=(f[i-1]+f[i-2])*(k-1);
    }
    cout<<f[n]<<endl;
    return 0;
}



附:时间复杂度O(logn)及空间复杂度为O(1)的算法:
Sure, you're welcome.
Okay, we're already know, that the answer can be found reccurently: F(N) = (K-1)*(F(K-1)+F(N-2)), where F(0) = 1, F(1) = K-1. We see, that the {F(N)} sequence is very similar to Fibonacci's one and can assume some facts. I see two different approaches to solve the problem.

1) First way is to build characteristic equation, solve it and get an exact formula for F(N). I didn't try this way, because I foresaw some problems with precision and float calculations. If you're interested, you can turn to this article as a manual:
http://www.intuit.ru/department/algorithms/algocombi/8/2.html

2) I assumed, that method with fast matrix exponentiation will work as well as for Fibonacci's numbers.
Just consider a matrix: L(N) = {{F(N+1), F(N)}, {F(N), F(N-1)}}.
Let's try to find such R, that L(N)*R = L(N+1).
If you write down this information, you'll easily see, that R = {{K-1, 1}, {K-1, 0}}. So all you need is to find L(1)*R.pow(N-2).

Hope this information was helpful, I'm not sure it is the best way to describe an approach though. :-)
分享到:
评论

相关推荐

    在线OJ网址大全在线OJ网址大全

    3. **Ural State University**(&lt;http://acm.timus.ru/&gt;) - **特点**:题库质量高,适合高级选手。 - **适用人群**:适合高级编程学习者。 4. **USACO Gate**(&lt;http://ace.delos.com/usacogate&gt;) - **特点**...

    Python库 | ural-0.28.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:ural-0.28.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    学习C语言的好网站和在线裁判系统 一些网站

    - 俄罗斯托木斯克国立大学(URAL):http://acm.timus.ru/ - 白俄罗斯国立大学(SGU):http://acm.sgu.ru/ - 哈尔滨工业大学(HIT):http://acm.hit.edu.cn/ - 湖南大学(HNU):...

    Покупки59.РФ-crx插件

    语言:русский 在网站上的联合购买的扩展59.rf shopping59.rf - 这是烫发中的一个联合...ural-toys.ru。我们还接受来自其他网站的订单:odezhda-master.ru。gepur.com.ua。milofo.ru。z29.ru.Butik-duhov.ru

    Pokupki59.RF「Покупки59.РФ」-crx插件

    在Shopping59.rf网站上进行联合购物扩张 Shopping59.RF - 这是在彼尔姆联合...ural-toys.ru 我们也接受来自其他网站的订单: odezhda-master.ru gepur.com.ua milofo.ru z29.ru butik-duhov.ru 支持语言:русский

    Ural URAL 解题思路

    《Ural URAL 解题思路》 在编程竞赛和算法挑战的世界中,Ural University的在线判题系统,简称Ural或URAL,是许多程序员磨炼技能的重要平台。它提供了丰富的算法题目,涵盖数据结构、图论、动态规划、数学等多个...

    Python库 | ural-0.25.0-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:ural-0.25.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    PART II THE URAL-ALTAIC HYPOTHESIS CHAPTER IV. THE URAL-ALTAIC HYPOTHESIS AND TUNGUS MATERIAL (1931年)

    28. The Theoretical Background of the Ural-Altale Hypothesis The hypothesis of common origin of the Altaic languages,and even the Ural-Altaic languages,dates from the first half of the last century....

    acm_ural_1148

    标题 "acm_ural_1148" 指向的是一个 ACM(国际大学生程序设计竞赛)问题,这个问题在 Ural University Online Judge(乌拉尔大学在线评测系统)上编号为 1148。描述中的 "Pascal acm_timus_ural_1148.pas" 表明提供...

    Ural

    "Ural"是一款字体,可能是指一种特定的中文字体或者西文字体设计。在IT领域,字体扮演着至关重要的角色,特别是在图形设计、网页设计、出版印刷以及各种视觉传达中。字体的选择能够极大地影响信息的呈现效果和用户...

    NOI数学(2021.09.28)B--69页.pdf

    - **指数原根**:对于给定的整数a和模p,如果存在一个正整数k使得ak ≡ 1 mod p,那么k就是a的指数原根。 - **欧拉函数**:φ(p)表示小于p且与p互质的正整数的数量,对于计算原根和模幂运算有重要应用。 在解决NOI...

    ACM练习题库

    URAL (俄罗斯乌拉尔大学在线题库)** - **官网**: http://acm.timus.ru - **特点**: 提供了丰富的俄罗斯本土题目。 - **适用人群**: 想要挑战更难问题的学习者。 **7. SGU (俄罗斯圣萨拉托夫州大学在线题库)** - **...

    URAL-PHA

    URAL-PHA是一个与字体相关的主题。在这个压缩包中,我们看到只有一个文件名“2238”,这可能是某种特定字体的文件或者一个包含了多个字体文件的集合。在IT行业中,字体设计是用户界面和视觉传达的重要组成部分,尤其...

    在线online judge

    **俄罗斯Ural立大学OnlineJudge(URAL)** - **网址**: http://acm.timus.ru/ - **特点**: URAL是另一个历史悠久的在线评测系统,虽然题库数量相对较少,但每一道题目都非常经典。对于那些想要深入理解算法和数据...

    URAL部分测试数据

    URAL部分测试数据是针对Timus Online Judge平台的一个竞赛或练习问题的数据集。Timus Online Judge是一个流行的在线编程竞赛和练习平台,它为参赛者提供了一系列的编程题目,以提升编程技能和算法理解能力。这个数据...

    URAL3D

    "URAL3D"是一个与字体相关的主题,这通常指的是一个特定的三维字体设计或字体库。在计算机领域,字体是用于显示和打印文本的图形表示。它们由一系列字符形状组成,包括字母、数字和标点符号,每个都有其独特的样式和...

    acm_ural_1099

    【标题】"acm_ural_1099" 是一个与编程竞赛相关的主题,通常在ACM(国际大学生程序设计竞赛)或类似的算法比赛中出现。这类问题旨在测试参赛者解决算法问题的能力,通常需要使用一种或多种编程语言来编写解决方案。 ...

    ACM网站大全(OJ+代码+贴吧)

    - **俄罗斯 (URAL)**:`http://acm.timus.ru/` - 俄罗斯的一个著名在线评测系统。 - **圣彼得堡国立大学 (SGU)**:`http://acm.sgu.ru/` - 圣彼得堡国立大学的在线评测系统。 - **莫斯科物理技术学院 (MIPT)**:`...

    ural部分题解

    "ural部分题解"是一个关于编程竞赛平台Ural(也称为URAL Online Judge或University of Maribor Algorithmic contests)的解题集,由大牛出品。这个资源包含Vol1到Vol3的部分题目解答,虽然不完整,但大约涵盖了200道...

    ural vol I 题解 by yuhch123 pdf

    ### Ural Volume I 题解 by yuhch123 #### 关于 yuhch123 yuhch123 是一位在 IT 和编程竞赛领域内有着卓越成就的人物,曾在 IOI 2008 中荣获金牌第一名。这份文档是他针对 Ural 编程题库第一卷的解答,对于学习算法...

Global site tag (gtag.js) - Google Analytics