【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G
问题 G: JS Window
时间限制: 2 Sec 内存限制: 512 MB
题目描述
At the begging, the Window is at the position 1 which means the Window contains the integers from position 1 to position M. Each time the Window move right by one until it reach the end. So after one move, the window contains position 2 to position M+1.
At each place, we can multiply all the integers in the window. Please calculate the product mod P at each position. As the output maybe very large, you should output the sum of these product.
For example, if the array is”2 3 7 11 13” with m=3 and P=5. So the product at position 1 is 2, at position 2 is 1 and at position 3 is 1. 2+1+1=4. So you should output 4.
输入
- The first line of the input contains three integers N,M,P (1 ≤ M≤ N≤ 1000000, 1 ≤ P≤ 1000000000), giving the length of the array, the length of the window and the number which we mod.
- The second line of the input contains four integers A[1],X,Y,Z(1 ≤ A[1],X,Y,Z≤ 1000000000). For i>1, A[i]=X*A[i-1]^2+Y* A[i-1]+Z
There are no more than 10 test cases. And the sum of N is no more than 10000000.
输出
样例输入
5 1 130495969
3 3 0 2
样例输出
84928588
meaning
n个数,每次取m个连续的数,求积模p再求和。
solution
想到了就很水的题
分块,每块长度为m,每块计算前缀积和后缀积
没有恰好落在块上的区间,可以看作前一块的后缀积×后一块的前缀积。
tips
和不要模p。
a[1]要模p。
code
#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
typedef long long ll;
ll n,m,p,x,y,z;
ll a[maxn];
ll ex[maxn],su[maxn];
int main() {
// IN_LB();
while(scanf("%lld%lld%lld",&n,&m,&p)!=EOF) {
scanf("%lld%lld%lld%lld",&a[0],&x,&y,&z);
a[0]%=p;
for(int i=1; i<n; i++) {
a[i] = (a[i-1]*a[i-1]%p*x%p+y*a[i-1]%p+z)%p;
}
for(int i=0; i<n; i++) {
if(i%m==0) {
ex[i] = a[i];
} else
ex[i] = a[i]*ex[i-1]%p;
}
for(int i=n-1; i>=0; i--) {
if((i+1)%m==0||i==n-1) {
su[i] = a[i];
} else
su[i] = a[i]*su[i+1]%p;
}
ll ans = 0;
for(int i=m-1; i<n; i++) {
if((i+1)%m==0) {
ans+=ex[i];
} else
ans+=(ex[i]*su[i-m+1])%p;
}
printf("%lld\n",ans);
}
return 0;
}
【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G的更多相关文章
- 【倍增】T-shirt @2018acm徐州邀请赛 I
问题 I: T-shirt 时间限制: 1 Sec 内存限制: 64 MB 题目描述 JSZKC is going to spend his vacation! His vacation has N ...
- 【容斥+组合数】Massage @2018acm徐州邀请赛 E
问题 E: Massage 时间限制: 1 Sec 内存限制: 64 MB 题目描述 JSZKC feels so bored in the classroom that he w ...
- bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- bootstrap历练实例:按钮作为输入框组前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 递归算法(二)——前缀转后缀
源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /**** ...
- POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)
给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...
- 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树
另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...
- 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理
一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...
- JS:window.onload的使用介绍
作者: 字体:[增加 减小] 类型:转载 时间:2013-11-13我要评论 window.onload在某些情况下还是比较实用的,比如加载时执行哪些脚本等等,下面有几个不错的示例,需要的朋友可以参考 ...
随机推荐
- [转] UniCode编码表
Unicode编码则是采用双字节16位来进行编号,可编65536字符,基本上包含了世界上所有的语言字符,它也就成为了全世界一种通用的编码,而且用十六进制4位表示一个编码,非常简结直观,为大多数开发者所 ...
- BZOJ3172 [Tjoi2013]单词 字符串 SA ST表
原文链接http://www.cnblogs.com/zhouzhendong/p/9026543.html 题目传送门 - BZOJ3172 题意 输入$n(n\leq 200)$个字符串,保证长度 ...
- BZOJ1911 [Apio2010]特别行动队 - 动态规划 - 斜率优化
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 UPD(2018-04-01):用Latex重打了公式…… 题意概括 把一个整数序列划分成任意连续的段,使得划分出 ...
- 067 HA与updateStateByKey结合
是HA与updateStateByKey相结合的程序. 有点问题,有点奇怪,重启项目后运行没有问题,但是第三次启动的时候,就不会在打印数据了,有点问题. 1.程序 package com.stream ...
- FileZilla FTP Client
FileZilla Client是一个快速.实用.多功能和界面直观的免费的FTP客户端,虽然它是免费软件,可功能却一点也不含糊,比起那些共享软件来有过之而无不及,在新的版本中作者改进了手动下载的界面和 ...
- day67 ORM模型之高阶用法整理,聚合,分组查询以及F和Q用法,附练习题整理
归纳总结的笔记: day67 ORM 特殊的语法 一个简单的语法 --翻译成--> SQL语句 语法: 1. 操作数据库表 创建表.删除表.修改表 2. 操作数据库行 增.删.改.查 怎么连数据 ...
- PageHelper在Mybatis中的使用
环境:Spring 4.2.1 Mybatis 3.2.8 pagehelper 5.1.2 Mybatis官方教程:https://github.com/pagehelper/Mybatis-Pag ...
- Fire! -两次dfs
题目描述: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap
最近在使用eclipse编写java程序时遇到这样一个问题: 错误在类中找不到main方法,请将main方法定义为 public static void main(String[] args)否则 J ...
- sqlserver日志文件
过程: 昨天下午数据库奔溃,表现就是连不上数据库了,重启服务之后好了. 查询日文文件 , “Autogrow of file 'XX_log' in database 'XX' was cance ...