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. Linux - 理不清的权限chmod与chown区别

    chmod是修改第一列内容的 ,chown是修改第3,4列内容的. [root@local ~]# chmod 777 -R add.sh [root@local ~]# chown jiqing:j ...

  2. Dragon Ball--hdoj

    Dragon Ball Problem Description Five hundred years later, the number of dragon balls will increase u ...

  3. 【HDU 1847】 Good Luck in CET-4 Everybody!

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1847 [算法] 我们知道,每一种状态,要么必胜,要么必败 记忆化搜索即可 [代码] #includ ...

  4. VisoStudio 允许局域网联机调试网站

    第一步:修改配置文件 添加IP访问配置 找到vs访问网站的端口后,添加一行新的配置 第二步:使用CMD命令进行网络配置 netsh http / user=everyone 删除网络配置的命令(注意最 ...

  5. Gym-101915C Shahhoud Training Hussain 模拟

    题面 题意:每天有K本书,你最多看P本一天,问N天后多少本书没有看 题解:ans=(K-P)*N; 注意一点就是P>=K的时候,ans=0; #include<bits/stdc++.h& ...

  6. Docker修改hosts方法

    方法一: 直接进入容器中修改/etc/hosts 缺点:重启容器后,增加的内容会丢失 方法二: 制作镜像的时候,直接修改. 限制: 需要是root用户,需要在容器中安装sudo 增大了镜像大小 方法三 ...

  7. Super超级ERP系统---(8)订单管理--订单创建

    订单管理是ERP系统中一个重要模块,客户下订单,ERP通过订单来为客户进行配送.订单模块主要包括订单创建,订单修改,订单审核,订单取消,订单分配,订单打印,订单拣货,订单出库.在随后的几节里我们看看这 ...

  8. 代理模式(Proxy)C++实现

    代理模式 尽管Decorator的实现部分与代理相似,但Decorator的目的不一样.Decorator为对象添加一个或多个功能,而代理则控制对对象的访问. 意图: 为其他对象提供一种代理以控制对这 ...

  9. 第7章 性能和可靠性模式 Server Clustering(服务器群集)

    上下文 您正在设计要部署应用程序的基础结构层.运行要求包括无法满足的可用性或性能能力,因为基础结构中存在性能瓶颈或故障单点. 影响因素 设计基础结构时,请考虑下列影响因素: 用户希望在使用应用程序时这 ...

  10. Linq怎么支持Monad

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