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次方
    */
  • 相关阅读:
    win10下python环境变量设置
    c++ primer第15章这几个例子中的构造函数形式不太理解
    ++与*
    C++符号优先级
    56-Remove Linked List Elements
    55. Binary Tree Preorder Traversal
    54. Flatten Binary Tree to Linked List
    野指针--内存泄漏--缓存区溢出--栈溢出
    数组指针和指针数组的区别
    53-Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9487549.html
Copyright © 2011-2022 走看看