Codeforces 1091D New Year and the Permutation Concatenation

   https://codeforces.com/contest/1091/problem/D

题目:

Let n be an integer. Consider all permutations on integers 1 to n in lexicographic order, and concatenate them into one big sequence p. For example, if n=3, then p=[1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1]. The length of this sequence will be n⋅n!n⋅n!.

Let 1≤i≤j≤n⋅n!be a pair of indices. We call the sequence (pi,pi+1,…,pj−1,pj) a subarray of pp. Its length is defined as the number of its elements, i.e., j−i+1. Its sum is the sum of all its elements, i.e., ∑jk=ipk.

You are given n. Find the number of subarrays of p of length n having sum n(n+1)/2. Since this number may be large, output it modulo 998244353 (a prime number).

Input

The only line contains one integer n (1≤n≤106), as described in the problem statement.

Output

Output a single integer — the number of subarrays of length nn having sum n(n+1)/2, modulo 998244353.

Examples

Input1

3

Output1

9

Input2

4

Output2

56

Input3

10

Output3

30052700

Note

  

In the first sample, there are 16 subarrays of length 3. In order of appearance, they are:

[1,2,3], [2,3,1], [3,1,3], [1,3,2], [3,2,2] [2,2,1], [2,1,3], [1,3,2], [3,2,3], [2,3,1], [3,1,3], [1,3,1], [3,1,2], [1,2,3], [2,3,2], [3,2,1].

Their sums are 6, 6, 7, 6, 7, 5, 6, 6, 8, 6, 7, 5, 6, 6, 7, 6. As n(n+1)/2=6, the answer is 9.

分析:

  这道题题意挺明确的,输入一个n,然后把长度为n,初始值为1的连续递增的数列进行全排列然后排在一块,再找其中连续的n个数,要求这n个数必须是1-n。

  题意就是这样那么就分析:

  暴力,呵呵呵呵,祝你好运

  看到只输入一个数n,第一反应就是oeis

  

  无果,遂自寻规律

  此处应该有规律

~10是
         

  当然这个在一开始做的时候是绝对绝对不知道的

  于是手推

  推大概n=5的时候395还没有太大的问题,分成第一个数字是12345可以分成五份,然后用全排列打个小程序,看看规律

  首先一个很显然的事实:

  当全排列第一个数字是1和全排列第一个数字是2的凑在一块,是肯定无法实现1-n的排列的

  所以我们可以把整体分为n份,此为第一个结论

  然后分析每一块

  先以n=3开始

  全排列是

  123132

  显然123 132 是两个,也就是组成这个大数列P的是肯定必须都满足条件

  然后就是231,此为第一个全排列的第二个数开始,这是重中之重

  第二个全排列的第二个数后面没有后继的数,遂作罢

  当n=4时,

  1234 1243 1324 1342 1423 1432

  首先显然的是6个

  其次就是每个全排列的第二个数和后面的n-1个数组成的都是成立的

  这些是5个

  然后每个全排列的第三个数开始的长度为n的数列成立的是3个

  这时候可能会有一丝丝思路,但不明确

  当n=5的时候

  写出来有点小多啊。。。。

  那就用程序跑

  截取我输出的结果

  


  首先的24个是跑不了

  然后接下来的23也跑不了

  再往下看,,就有些神奇了

  此时应该关注每个全排列的第三个数

  第三个数和第四第五个数,用竖着看的方式,会发现当前两个数固定,就是后面在进行全排列!(废话这是全排列的性质)

  那么我们可以分析出来这24个中,5个成立的,1个不成立的,5个成立的,1个不成立的

  咦居然有周期

  试试第四个数

  1,1,1,1,1

  也就是成立和不成立交叉着

  那么和第四个数相匹配,那是什么?

  23+1 = 24

  5+1 = 6

  1+1 = 2

  这不就是阶乘吗!

  然后再用心钻研那么一点点

  ((n-1)!+(n-1)!*((n-2)!-1)/(n-2)!+(n-1)!*((n-3)!-1)/(n-3)!···)*n

  讲真这里还不如自己手写一下然后看代码

  对了除法取模要用逆元

  

  这道题你要说不会做以后遇到怎么办,我只能说多练数学题,培养对数学的感觉,就是这样

 AC代码

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define LL long long
#define eps 1e-6
using namespace std;
const ll mod = ;
ll pow_quick(ll a,ll b){
ll r = ,base = a;
while(b){
if(b & ){
r *= base;
r %= mod;
}
base *= base;
base %= mod;
b >>= ;
}
return r;
} ll a[];
int main() {
ll n;
sld(n)
if(n == ){
p() pn return ;
}
a[] = 1ll;
for(ll i = ; i <= n; i ++){
a[i] = a[i-]*i;
a[i] %= mod;
}
ll sum = a[n-];
//pld(sum) pn
for(ll i = ; i <= n; i ++){
ll x = (a[n-]*pow_quick(a[i-],mod-))%mod;
x *= a[i-]-;
x %=mod;
sum += x;
sum %= mod;
}
pld((sum*(n))%mod) pn
return ;
}

  代码链接:https://codeforces.com/contest/1091/submission/47758982

  

Codeforces 1091D New Year and the Permutation Concatenation 找规律,数学 B的更多相关文章

  1. Codeforces D. Little Elephant and Interval(思维找规律数位dp)

    题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...

  2. Codeforces Good Bye 2018 D (1091D) New Year and the Permutation Concatenation

    题意:给n!个n的排列,按字典序从小到大连成一条序列,例如3的情况为:[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1],问其中长度为n,且和为sum=n*(n+1) ...

  3. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  4. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

  5. codeforces#1136 C. Nastya Is Transposing Matrices(找规律)

    题意:给出两个n*m的矩阵,每次操作可以让一个正方形矩阵行列交换.问,在无限次操作下,第一个矩阵能否变成第二个矩阵 分析:先把操作限定在2*2的矩阵中.这样对角线上的元素就可以随意交换.也就是说,如果 ...

  6. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  7. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  8. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  9. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

随机推荐

  1. 为什么对string调用swap会导致迭代器失效

    一般来说,swap操作将容器内容交换不会导致容器的指针.引用.迭代器失效. 但当容器类型为array和string时除外. 原因在于:SSO  (Short String Optimization 指 ...

  2. 前端笔记-javaScript-2

    一.JavaScript的对象 简介: 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array. ...

  3. CentOS 6.5 64位下安装Redis3.0.2的具体流程

    系统环境:CentOS 6.5 64位 安装方式:编译安装 防火墙:开启 Redis版本:Redis 3.0.2 一.环境准备 1.安装 gcc gcc-c++ [root@iZ94ebgv853Z ...

  4. 循环输入到列表的基础方法 -----python-----

    print('向列表中添加元素(输入“#”结束)\n并查看添加完的列表') list1=[] while 1: username=input('>>>') if (username. ...

  5. 云笔记项目-Spring事务学习-传播MANDATORY

    接下来测试事务传播属性MANDATORY Service层 所有Service层实现类都设置事务传播属性为MANDATORY. LayerT层代码 package LayerT; import jav ...

  6. Excel VBA 连接各种数据库(二) VBA连接Oracle数据库

    本文主要内容: Oracle环境配置 ODBC驱动设置.第三方驱动下载 VBA连接Oracle连接方法 Oracle10g官方免账号下载地址 系统环境: Windows 7 64bit Excel 2 ...

  7. css第三天

    三 1.标准模式与怪异模式(针对盒子模型)与边框,填充,边界,有关 标准模式(默认):元素的总宽度 = 盒子的宽度 + 左右填充宽度 + 左右边框宽度元素的总高度 = 盒子的高度 + 上下填充高度 + ...

  8. selenium java 浏览器操作

    环境搭建 selenium 2.53 selenium-java-2.53.0.jar selenium-java-2.53.0-srcs.jar 原代码包 拷贝的工程lib下,做build path ...

  9. Android 8.0+ 通知不显示的适配

    最近在 写项目的时候  发现 通知并不会显示的问题,查看资料发现 从Android 8.0开始通知必须加上ChannelId Android O 引入了 通知渠道(Notification Chann ...

  10. Linux驱动之LCD驱动编写

    在Linux驱动之内核自带的S3C2440的LCD驱动分析这篇博客中已经分析了编写LCD驱动的步骤,接下来就按照这个步骤来字尝试字节编写LCD驱动.用的LCD屏幕为tft屏,每个像素点为16bit.对 ...