Johnny is a brilliant mathematics student. He loves mathematics since he was a child, now he is working on his PhD thesis. He faces a small mathematical problem, he has a n digit integer number (let us call it s) , he wants to find how many substring of s are divisible by a prime number p.

His supervisor professor is on vacation now, so can you play his role and help him with that?

https://cn.vjudge.net/contest/174050#problem/K

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
#define ref(i,x,y)for(int i=x;i<=y;++i)
#define def(i,x,y)for(int i=x;i>=y;--i)
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pai1;
const double EPS=1e-;
const double PI=acos(-);
//next_permutation
int T,n,p,number[],s[],t[];
unsigned long long a,b;
int main()
{
scanf("%d",&T);
ref(i,,T)
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout);
cin>> a >> b >> n >> p;
ref(i,,n)
{
number[i]=a*/b;
a=a*;
a%=b;
}
if(p==||p==||p==)
{
long long ans=;
ref(i,,n)if(number[i]%p==)ans+=i;
cout<< ans<< endl;
continue;
}
int tmp=;
s[n+]=;
def(i,n,)
{
s[i]=(s[i+]+tmp*number[i])%p;
tmp=tmp*%p;
}
ref(i,,p)t[i]=;
def(i,n+,)t[s[i]]++;
long long ans=;
ref(i,,p-)
if(t[i]>)ans+=1LL*t[i]*(t[i]-)/;
cout<< ans<< endl;
}
}

最新文章

  1. React native 的弹出层(输入)效果
  2. Python 操作 MySQL 之 pysql 与 ORM(转载)
  3. RabbitMQ官方中文入门教程(PHP版) 第三部分:发布/订阅(Publish/Subscribe)
  4. C#制作验证码
  5. html 框架
  6. react input 获取/失去焦点
  7. vmware-workstation-11中centos-6.6安装
  8. centos 卸载vsftpd方法
  9. 在Hibernate中分别使用JDBC和JTA事务的方法
  10. 设置 cell点击 背景色
  11. AJAX 表单提交 文件上传
  12. CCF认证考试——折点计数
  13. 决策树系列(四)——C4.5
  14. Linux 下的分屏利器-tmux安装、原理及使用
  15. 【BZOJ3451】Normal
  16. 搭建mxnet-gpu docker的pyhon remote kernel
  17. iOS更换科大讯飞的key
  18. [leetcode53]最长子数组 Maximum Subarray Kadane&#39;s算法
  19. 20165313 预备作业3 Linux安装及学习
  20. Windows 8 禁用强制驱动签名

热门文章

  1. sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) Can&#39;t reconnect until invalid transaction is rolled back
  2. web开发(五) JSP详解(四大作用域九大内置对象等)
  3. delphi中 formclose的事件 action:=cafree form:=nil分别是什么意思?
  4. 【css】常用的几种水平垂直居中方式与盒子模型,面试经常问到!
  5. How do I add a Foreign Key Field to a ModelForm in Django?
  6. HtML5与CSS3基础
  7. Pytorch实现Top1准确率和Top5准确率
  8. 换根dp特征总结
  9. (转)在Kubernetes集群中使用JMeter对Company示例进行压力测试
  10. python 并发编程 异步IO模型