问题 G: JS Window

时间限制: 2 Sec  内存限制: 512 MB

题目描述

JSZKC has an array A of N integers. More over, he has a Window of length M which means the Window can contain M continuous integers in the array. 
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 input file contains several test cases, each of them as described below.

  • 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.

输出

One line per case, an integer indicates the answer

样例输入

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的更多相关文章

  1. 【倍增】T-shirt @2018acm徐州邀请赛 I

    问题 I: T-shirt 时间限制: 1 Sec  内存限制: 64 MB 题目描述 JSZKC is going to spend his vacation! His vacation has N ...

  2. 【容斥+组合数】Massage @2018acm徐州邀请赛 E

    问题 E: Massage 时间限制: 1 Sec  内存限制: 64 MB 题目描述 JSZKC  feels  so  bored  in  the  classroom  that  he  w ...

  3. bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  4. bootstrap历练实例:按钮作为输入框组前缀或后缀

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. 递归算法(二)——前缀转后缀

    源码:pretopost.cpp #include "stdafx.h" #include <stdio.h> #include <stack> /**** ...

  6. POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)

    给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...

  7. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

  8. 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 '*' 的处理

    一.删除前缀 '*' #include<iostream> #include<cstdio> using namespace std; //主函数 int main() { ] ...

  9. JS:window.onload的使用介绍

    作者: 字体:[增加 减小] 类型:转载 时间:2013-11-13我要评论 window.onload在某些情况下还是比较实用的,比如加载时执行哪些脚本等等,下面有几个不错的示例,需要的朋友可以参考 ...

随机推荐

  1. 利用 Windows API Code Pack 修改音乐的 ID3 信息

    朋友由于抠门 SD 卡买小了,结果音乐太多放不下,又不舍得再买新卡,不得已决定重新转码,把音乐码率压低一点,牺牲点音质来换空间(用某些人的话说,反正不是搞音乐的,听不出差别)… 结果千千静听(百度音乐 ...

  2. webstorm ps

    2018WebStorm注册码   2018-10-10 2018年08月22日 17:36:58 阳光明媚的味道 阅读数:6325   8月21日 http://webstorm.autoseasy ...

  3. 【bzoj4347】[POI2016]Nim z utrudnieniem dp

    题解: 感觉我简直是个傻逼 把题目数据范围看错了.. 然后觉得这题非常的不可做 sigmaai <1e7.... 这题的dp是非常简单的,注意到d很小 f[i][j][k]表示前i个,%d为j, ...

  4. UIActionSheet的常用方法

    enum UIActionSheetStyle : Int { case Automatic // take appearance from toolbar style otherwise uses ...

  5. WPF中矢量图制作和引用

    WPF程序有时需要适配不同大小的屏幕,图片是像素图的话,拉伸之后会变模糊,影响美观度,因此可以考虑使用矢量图.网上找到了相应的介绍:http://learnwpf.com/post/2006/06/0 ...

  6. mongodb输错命令后不能删除问题

    在用crt连接Linux操作MongoDB时,命令输错了,想删除的时候,却删除不了,原因是crt的配置有问题,解决办法如下 第一步:选项-->会话选项

  7. 修改ini文件的批处理

    用VBS更简单: vbs代码: On Error Resume Next Dim Fso,TxtFl,Str Set Fso = CreateObject("Scripting.FileSy ...

  8. net core体系-API-1Ocelot-(2)继续深入

    简单的说Ocelot是一个用.NET Core实现并且开源的API网关技术.可能你又要问了,什么是API网关技术呢?Ocelot又有什么特别呢?我们又该如何集成到我们的asp.net core程序中呢 ...

  9. Ajax+PHP实现异步上传多张图片

    Ajax+PHP实现异步上传多张图片 HTML代码 <!-- date: 2018-04-27 13:46:55 author: 王召波 descride: 多张图片上传 --> < ...

  10. 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...