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. Office 365 Licence使用情况统计

    负责采购的同事需要知道目前公司使用了多少License,但是通过O365控制台界面似乎无法直接导出License使用量的信息,于是让我帮忙从后台统计一下. $mail_text = Read-Host ...

  2. 移动web 公用样式

    /*! normalize.css v3.0.2 | MIT License | github.com/necolas/normalize */ /** * 1. Set default font f ...

  3. 关于string.format() 转

    string.format()函数用来生成具有特定格式的字符串,这个函数有两个参数,第一个参数为格式化串:由指示符和控制格式的字符组成.第二个参数是对应格式中每个代号的各种数据. 格式字符串可能包含以 ...

  4. 使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...

  5. sleep thread process

    $str = 'w0'; for($w=0;$w<999999;$w++){ $sql = 'INSERT INTO w1 (wint,wchar) VALUES ('.$w.',"' ...

  6. VC中对文件的读写

    http://www.cnblogs.com/LJWJL/archive/2012/10/06/2712466.html 注意: 1.由于C是缓冲写 所以要在关闭或刷新后才能看到文件内容 2.电脑处理 ...

  7. java连接mysql数据库详细步骤解析

    java连接mysql数据库详细步骤解析      第一步:下载一个JDBC驱动包,例如我用的是:mysql-connector-java-5.1.17-bin.jar      第二步:导入下载的J ...

  8. Android中,Context,什么是Context?

    注:本文翻译自Context, What Context?,原文链接在这里,作者是Dave Smith.ps:译者链接http://blog.csdn.net/race604/article/deta ...

  9. 《javascript高级程序设计》第三章学习笔记

    Undefined类型 该类型只有一个值,即undefined. 对未初始化的变量和未定义的变量,用typeof检测,都会返回'undefined' Null类型 该类型只有一个值,null.并且从逻 ...

  10. JMeter学习-037-JMeter调试工具之四-BeanShell+jmeter.log

    前面三篇文章分别讲述了 HTTP Mirror Server . Debug PostProcessor 和 Debug Samper 的脚本调试实例.此文主要讲述第四种调试方法,通过 BeanShe ...