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. lightoj--1005--Rooks(组合数)

    Rooks Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status De ...

  2. poj--2553--The Bottom of a Graph (scc+缩点)

    The Bottom of a Graph Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Oth ...

  3. 框架,表格,表单元素,css基础以及基本标签的结合

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  4. 不用任何插件,实现一个tab栏切换

    //使用jquery中获取当前索引的方法.显示隐藏 <script> $(".tab_list li").on('click', function () { $(thi ...

  5. MAVEN 杂记

    MAVEN中央仓库 http://repo.maven.apache.org/maven2http://repo1.maven.org/maven2/http://mvnrepository.com/ ...

  6. caffe 参数介绍 solver.prototxt

    转载自 http://blog.csdn.net/cyh_24/article/details/51537709 solver.prototxt net: "models/bvlc_alex ...

  7. IO流读取文件内容时,出现空格的问题(未找到原因)

    import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExceptio ...

  8. Oracle-基本SQL语句

    --添加一个表 create table TestUser ( id int primary key , name varchar(20) , address varchar(20) ) /* *设置 ...

  9. Linq怎么支持Monad

    在上一篇创建了我们的第一个Monad, Identity<T>. 我们确定了类型要变成Monad, 它必须有一个type constructor(Identity<T>), 和 ...

  10. 【Oracle】利用trace文件重建控制文件

    重建步骤: 1.生成trace文件: alter database backup controlfile to trace; 2.找到生成的trace文件: show parameter dump u ...