zoukankan      html  css  js  c++  java
  • 1360: Good Serial Inc.(不知道是什么类型的题)

    1360: Good Serial Inc.

          Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 172     Solved: 38    


    Description

    Good Serial Inc. (GSI) produces integer sequences with length N, in which every element is an integer in range [1, M].
    They call a sequence S is good if the sequence has the following property: for every substring of S with length M, i.e., S[i→i+m-1], all the elements in the substring are the same or all the elements are distinct(different from each other).
    The company GIS is designed to produce good sequences. But how many different good sequences are there? Since the answer will be very large, just output the result after module 987654321.

    Input

    There are several cases. For each case, there is a line with two integers N, and M ( 1 ≤ N ≤ 1000000, 1 ≤ M ≤ 1000000 ).
    The input ends up with two negative numbers, which should not be processed as a case.

    Output

    Print the number of different good sequences module 987654321 in a line for each case.

    Sample Input

    4 4
    3 5
    -1 -1

    Sample Output

    28
    125

    Hint

    Source

    做法:
    1.当m==1的时候,序列全部由相同的1组成
    2.m==2的时候,对序列的每个位置都可以有两种情况,所以是m的n次方

    注意m==2要放在n>=m的情况中讨论
    3.n>=m m长度的子串全部相同的情况:m种,全1到m
        m长度的子串全部不同的情况:m的全排列 所以是m的阶乘
        所以是二种情况的总和
    4.n<m 也是m的n次方

    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<algorithm>
    #include<memory.h>
    #include<memory>
    using namespace std;
    #define mod 987654321
    typedef long long LL;
    LL f(int n,int m)
    {
        LL ans=1;
    
        if(m==1)
            return 1;
        if(n>=m)
        {
            if(m==2)
            {
                for(int i=1;i<=n;i++)
                    ans=ans*m%mod;//m的n次方
            }
            else
            {
                for(int i=1;i<=m;i++)
                    ans=(ans*i)%mod;//m的阶乘
                ans+=m;
            }
            return ans;
        }
        else if(n<m)
        {
            for(int i=1;i<=n;i++)
                ans=ans*m%mod;//m的n次方
            return ans;
        }
    }
    int main()
    {
        int n,m;
        while(~scanf("%d %d",&n,&m))
        {
            if(n<0&&m<0)
                break;
            printf("%lld
    ",f(n,m));
        }
        return 0;
    }
    /*
    做法:
    1.当m==1的时候,序列全部由相同的1组成
    2.m==2的时候,对序列的每个位置都可以有两种情况,所以是m的n次方
    3.n>=m m长度的子串全部相同的情况:m种,全1到m
        m长度的子串全部不同的情况:m的全排列 所以是m的阶乘
        所以是二种情况的总和
    4.n<m 也是m的n次方
    */
  • 相关阅读:
    Rational全系列工具介绍
    转贴 MM(ModelMaker)建模工具快速上手指南delphi
    eclipse打不开报(Failed to create the Java Virtual Machine)解决方法
    Vagrant系列(二)Vagrant的配置文件Vagrantfile详解
    Xshell登录Vagrant方式
    win10系统在执行“ vagrant box add centos7 vagrantcentos7.box”添加box时,报错“Vagrant failed to initialize at a...
    win10系统搭建vagrant时开启bios,虚拟化问题
    查看memcache版本
    python空为None
    python 获得字符串长度
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9487549.html
Copyright © 2011-2022 走看看