X problem

    

X问题

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4358 Accepted Submission(s): 1399

Problem Description
    求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。
 
Input
    输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。
 
Output
    对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。
 
Sample Input
3
10 3
1 2 3
0 1 2
100 7
3 4 5 6 7 8 9
1 2 3 4 5 6 7
10000 10
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9

  

 
Sample Output
1
0
3
 
Author
lwg

 

Source
 
   水题一道……
   显然暴力不行,直接上来中国剩余定理yy一发,求出最小解后直接依次加上最小公倍数就可以了
 
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
int ex_gcd(int a,int b,int &x,int &y){//扩展欧几里得
if(b==0){
x=1,y=0;
return a;
}
int k=ex_gcd(b,a%b,x,y);
int tmp=x;
x=y;
y=tmp-a/b*y;
return k;
}
int T;
int N,M;
int w[11],r[11];
int China(int N){
int M=w[1],R=r[1];
int x,y;
for(int i=2;i<=N;i++){
int d=gcd(M,w[i]);
int c=r[i]-R;
if(c%d) {return -1;}
ex_gcd(M/d,w[i]/d,x,y);
x=(c/d*x)%(w[i]/d);
R+=x*M;
M=M/d*w[i];
R%=M;
}
if(R<0) return R+M;
else return R;
}
int main(){
T=read();
while(T--){
N=read(),M=read();
for(int i=1;i<=M;i++) w[i]=read();
for(int i=1;i<=M;i++) r[i]=read();
int ret=China(M);
if(ret==-1||ret>N) {puts("0");continue;} //特判
int lcm=1,ans=1;
for(int i=1;i<=M;i++) lcm=lcm*w[i]/gcd(lcm,w[i]);//求所有数的最小公倍数,有个式子是a*b=gcd(a,b)*lcm(a*b);
while(ret+lcm<N){
ans++;
ret+=lcm;
}
cout<<ans<<endl;
}
}

【数论】X problem的更多相关文章

  1. 【题解】CF986E Prince's Problem(树上差分+数论性质)

    [题解]CF986E Prince's Problem(树上差分+数论性质) 题目大意: 给定你一棵树,有点权\(val_i\le 10^7\).现在有\(m\)组询问给定参数\(x,y,w\)问你对 ...

  2. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  3. UVa 1363 (数论 数列求和) Joseph's Problem

    题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...

  4. 【数论】FOJ 2238 Daxia & Wzc's problem

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...

  5. BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  6. UVA 11490 - Just Another Problem(数论)

    11490 - Just Another Problem option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  7. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

    题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...

  9. CF45G Prime Problem 构造+数论

    正解:构造+数论 解题报告: 传送门! maya这题好神仙啊我jio得,,,反正我当初听的时候是没有太懂的,,, 首先这题你要知道一些必要的数学姿势 比如哥德巴赫猜想巴拉巴拉的 然后直接讲题趴QAQ ...

随机推荐

  1. yxcms后台验证码不显示?怎么取消yxcms后台验证码

    嗨,大家好,我是YXCMS的小M老湿,(其实还是习惯大家叫我猪猪吧!)今天又要分享一则yxcms的使用技巧,当然也是yxcms用户在使用过程中很容易出现的小白问题,当然还是同样,yxcms的大神级别的 ...

  2. Blog 公用部分结构与class定义

    /*博客文章公用部分class与结构 common*/ /* 1.title-block //标题块 ├── border-danger //危险红 ├── border-info //普通蓝 └── ...

  3. Redhat 一则关于路由及DNS配置的实例

    安装了Redhat 7.2, 配置路由, 但发现路由重启之后,不能生效. 配置路由: touch /etc/sysconfig/static-router, 然后编辑路由信息如下. any defau ...

  4. asp.net mvc 在视图中获取控制器与动作的名称

    获取 controller 名称: ViewContext.RouteData.Values["controller"].ToString(); 获取 action 名称: Vie ...

  5. sprint3总结

    经过了半个学期以来的sprint冲刺,并且充分学习了android开发后对项目有了更加充分的认识理解,开发速度自然而然就上来了,没有了上一个学期的懵懂,虽然开发起来还是比较困难,但是胜在有同组组员帮忙 ...

  6. MySql连接Visual studio Code First插件

    到mySql官网Downloads==> MySQL on Windows==>MySQL for Visual Studio 下载插件安装即可

  7. 关于IOS音频的开发积累

    1.设置类别,表示该应用同时支持播放和录音 OSStatus error; UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; ...

  8. Hadoop - Unable to load native-hadoop library for your platform

    简介 运行hadoop或者spark(调用hdfs等)时,总出现这样的错误“Unable to load native-hadoop library for your platform”,其实是无法加 ...

  9. 临时存存储页面上的数据---Web存储

    HTML5 Web存储的两种方法使用 localStorage和sessionStorage 参考: http://www.cnblogs.com/taoweiji/archive/2012/12/0 ...

  10. Linux-002-执行命令时,提示: -bash: {命令}: command not found

    首先,此文不适应未安装的命令. 起因: 进行系统环境变量配置时,路径分割符配置错误,错将":"配置为";". 现象: 任意用户执行命令时,提示:command ...