Time Limit:3000MS Memory Limit:65536KB

Description You are given a sequence a[0]a[1] ... a[N-1] of digits and a prime number Q. For each i<=j with a[i] != 0, the subsequence a[i]a[i+1]...a[j] can be read as a decimal representation of a positive integer. Subsequences
with leading zeros are not considered. Your task is to count the number of pairs (i, j) such that the corresponding subsequence modulo Q is R. Input The input consists of at most 10 datasets. Each dataset is represented by a line containing four integers N,
S, W, Q, and R, separated by spaces, where 1<=N<=10^5, 1<=S<=10^9, 1<=W<=10^9, Q is a prime number less than 10^9 and 0<=R<Q. The sequence a[0]...a[N-1] of length N is generated by the following code.

int g = S;

for (int i=0; i<N; i++) {

a[i] = (g/7) % 10;

if( g%2 == 0 ) { g = (g/2); }

else { g = (g/2) ^ W; }

}

Note: the operators /, %, and ^ are the integer division, the modulo, and the bitwise exclusiveor, respectively. The above code is meant to be a random number generator. The intended solution does not rely on the way how the
sequence is generated. The end of the input is indicated by a line containing five zeros separated by spaces. Output For each dataset, output the answer in a line.

Sample Input

3 32 64 7 0

4 35 89 5 0

5 555 442 3 0

5 777 465 11 0

100000 666 701622763 65537

0 0 0 0 0 0

Sample Output

2

4

6

3

68530

Hint In the first dataset, the sequence is 421. We can find two multiples of Q = 7, namely, 42 and 21. In the second dataset, the sequence is 5052, from which we can find 5, 50, 505, and 5 being the multiples of Q = 5. Notice
that we don't count 0 or 05 since they are not a valid representation of positive integers. Also notice that we count 5 twice, because it occurs twice in different positions. In the third and fourth datasets, the sequences are 95073 and 12221, respectively.

题目大意

给出一串十进制的数字,求当中有多少个子串所表示的数字模质数p的值为r

思路

如果一个子串为x[i]x[i+1]...x[j],则模p的值为(x[i]*10^(j-i)+x[i+1]*10^(j-i-1)+...+x[j]*10^0)%p,如果用f[n]表示(10^n)%p, t[n]表示原串的后n位模p的值。那么x[i]x[i+1]...x[j] %p=r等价于t[i]=(t[j-1]+r*10^(j-1))%p=(t[j-1]+r*f[j-1])%p,((a/b)%c=(a%(b*c)),这样以第i位开头的数字中符合要求的个数即为符合该式的j的个数(j>=i)。

能够用map或哈希记录个数。m[x]表示符合(t[j]+r*f[j])%p=x的j的个数。

做法

按题目给的方法生成数字串,预处理求出f[n],再从右向左依次处理出t[n],将答案加上m[t[n]],再更新m[(t[n]+r*f[n])%p]就可以。

说明

当题目中的质数为2或5时须要特殊处理,由于10能够被2或5整除,不能再用10的幂次的形式取余。此时仅仅须要从高位向地位。不断统计最后一位模p为r的子串个数就可以,由于一个数除2或5的余数就等于最后一位模2或5.

数字的首位不能为0,所以要注意仅仅有当前位不为0时才干更新答案。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int N=100005;
long long a[N];
long long f[N];
void init(int n,long long s,long long w)
{
long long g=s;
for (int i=1; i<=n; i++)
{
a[i]=(g/7)%10;
if (g%2==0) g=g/2;
else g=(g/2)^w;
}
}
int main()
{
int n,i;
long long s,w,q,r;
long long x,y,ans;
while (scanf("%d%lld%lld%lld%lld",&n,&s,&w,&q,&r)==5)
{
if (n==0 && s==0 && w==0 && q==0 && r==0) break;
init(n,s,w); ans=0;
if (q==2 || q==5)
{
s=0;
for (i=1; i<=n; i++){
if (a[i]!=0) s++;
if (a[i]%q==r) ans+=s;
}
}
if (q!=2 && q!=5)
{
f[n]=1;
for (i=n-1; i>=1; i--) f[i]=(f[i+1]*10)%q;
map<int,int> m;
m[r]=1;
x=0;
for (i=n; i>=1; i--)
{
x=(x+a[i]*f[i])%q;
if (a[i]!=0)ans+=m[x];
m[(r*f[i-1]+x)%q]++;
}
}
printf("%lld\n",ans);
}
return 0;
}

数论(同余+hash)的更多相关文章

  1. LightOJ1214 Large Division 基础数论+同余定理

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  2. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 51nod 1433 0和5【数论/九余定理】

    1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 小K手中有n张牌,每张牌上有一个一位数的数,这个 ...

  4. FZU 1057 a^b 【数论/九余定理】

    Accept: 1164    Submit: 3722Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 对于任 ...

  5. 数论 - 同余 + BFS (Find The Multiple)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16995   Accepted: 692 ...

  6. 【斐波拉契+数论+同余】【ZOJ3707】Calculate Prime S

    题目大意: S[n] 表示 集合{1,2,3,4,5.......n} 不存在连续元素的子集个数 Prime S 表示S[n]与之前的所有S[i]互质; 问 找到大于第K个PrimeS 能整除X 的第 ...

  7. 数论同余学习笔记 Part 2

    逆元 准确地说,这里讲的是模意义下的乘法逆元. 定义:如果有同余方程 \(ax\equiv 1\pmod p\),则 \(x\) 称为 \(a\bmod p\) 的逆元,记作 \(a^{-1}\). ...

  8. 分布式理论(八)—— Consistent Hash(一致性哈希算法)

    前言 在分布式系统中,常常需要使用缓存,而且通常是集群,访问缓存和添加缓存都需要一个 hash 算法来寻找到合适的 Cache 节点.但,通常不是用取余hash,而是使用我们今天的主角-- 一致性 h ...

  9. 一致性hash算法--负载均衡

    有没有好奇过redis.memcache等是怎么实现集群负载均衡的呢? 其实他们都是通过一致性hash算法实现节点调度的. 讲一致性hash算法前,先简述一下求余hash算法: hash(object ...

随机推荐

  1. 国外物联网平台初探(五) ——Exosite Murano

    定位 Murano是一个基于云的IoT软件平台,提供安全.可扩展的基础设施,支持端到端的生态系统,帮助客户安全.可扩展地开发.部署和管理应用.服务以及联网产品. 功能 Murano平台简化了整个IoT ...

  2. python 读取二进制文件 转为16进制输出

    示例: #!/usr/bin/env python #encoding: utf-8 import binascii fh = open(r'C:\Temp\img\2012517165556.png ...

  3. hdoj-1827-Summer Holiday(scc+缩点)

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. AIX的系统备份

    AIX克隆盘即AIX的rootvg的备用替换磁盘,用于保留AIX的原始状态,它可作为软件的升级后出现问题快速回退到原系统的备份手段,也可用于测试两个不同版本的AIX系统.系统可保留两块引导磁盘,而且都 ...

  5. 【docker】python: can't open file 'helloworld.py': [Errno 13] Permission denied

    运行容器提示权限问题 docker run  -v $PWD/myapp:/usr/src/myapp  -w /usr/src/myapp python:3.5 python helloworld. ...

  6. 移动端rem设置(部分安卓机型不兼容)

    (function(win) { var doc = win.document; var docEl = doc.documentElement; var tid; function refreshR ...

  7. 使用原生JS实现简单的ajax

    Ajax是一种使用javascript内置对象向服务器发送请求/接收响应的技术.它可以再页面已经完全显示出来之后再和服务器进行少量的数据交互,所以可以实现局部内容的刷新. ajax的实现,主要是靠ja ...

  8. MFC知识点总结

    一:消息1.什么是消息?消息是驱动windows系统运行的基础.从计算机的角度来看,消息就是一个整数.    (1)一个无符号整数,是消息值:    (2)消息附带的WPARAM和LPARAM类型的参 ...

  9. C# HttpWebRequest post 请求传参数

    Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 paramet ...

  10. 解决strip: Unable to recognise the format of the input file问题

    前言   在编译xilinx的uboot的时候出现了一个问题,始终报错:“strip: Unable to recognise the format of the input file `gen_et ...