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. Android 开发项目笔记2 自建View满足特殊要求

    写java文件组成特殊的View,然后在xml文件中通过 包名.类名 调用. 1.padding 是指的组件内部的填充区域,不是指组件与parent之间的区域.操控后者间距后者使用margin 2.使 ...

  2. R爬虫知识点

    >>如何用 R 模仿浏览器的行为? GET / POST URLencode / URLdecode (破解中文網址的祕密) header & cookie 如何突破使用 cook ...

  3. HTML5的视频和音频

    1.HTML5视频 HTML5 规定了一种通过 video 元素来包含视频的标准方法. <!DOCTYPE html> <html> <head> <meta ...

  4. 使用TouchScript做2D按钮实现长按功能

    导入TouchScript 下载地址:https://www.assetstore.unity3d.com/#/content/7394 把TouchScript和Touch Debugger两个预设 ...

  5. JS动态引入js,CSS——动态创建script/link/style标签

    一.动态创建link方式 我们可以使用link的方式.如下代码所示. 二.动态创建style方式 但是,这样的话,需要加载整个css文件,但是那样有可能浪费一个http请求并占用一个服务器请求数,并等 ...

  6. Python3中使用PyMySQL连接Mysql

    Python3中使用PyMySQL连接Mysql 在Python2中连接Mysql数据库用的是MySQLdb,在Python3中连接Mysql数据库用的是PyMySQL,因为MySQLdb不支持Pyt ...

  7. Neil·Zou 语录一

    1  既然选择了远方 Since I’ve chosen to go far    便只顾风雨兼程 I will just walk down the path I chose step by ste ...

  8. Yii源码阅读笔记(三十)

    Widget类是所有小部件的基类,开始,结束和渲染小部件内容的方法的注释: namespace yii\base; use Yii; use ReflectionClass; /** * Widget ...

  9. java beans

    There are N little kids sitting in a circle, each of them are carrying some java beans in their hand ...

  10. 关于 windows 下 node_modules\node-sass\vendor 的报错解决方法

    项目git clone下来之后,运行npminstall, npm start报错代码如下: ERROR in ENOENT: no such file or directory, scandir ' ...