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次方
    */
  • 相关阅读:
    VS插件哪家强?CodeRush v20.2帮你忙
    WinForms界面开发工具DevExpress WinForms v20.2亮点——全新Sankey Diagram控件震撼发布
    java中将信息写入excel
    java中使用IO流将以文件中的内容去取到指定的文件中
    java中使用IO流复制文件
    采购订单写入sap失败后,抛出自定义异常,回滚数据库
    java中将文件夹里面的文件复制到指定的文件夹(java IO)
    JAVA中IO流详解
    获取员工合同信息列表 定时任务
    Java连接MySQL数据库——含步骤和代码
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9487549.html
Copyright © 2011-2022 走看看