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.
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\n",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次方 */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南