【数论】X problem
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
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
1
0
3
#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的更多相关文章
- 【题解】CF986E Prince's Problem(树上差分+数论性质)
[题解]CF986E Prince's Problem(树上差分+数论性质) 题目大意: 给定你一棵树,有点权\(val_i\le 10^7\).现在有\(m\)组询问给定参数\(x,y,w\)问你对 ...
- [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& ...
- 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的整数部分 ...
- 【数论】FOJ 2238 Daxia & Wzc's problem
题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...
- BZOJ 2301: [HAOI2011]Problem b( 数论 )
和POI某道题是一样的... http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...
- UVA 11490 - Just Another Problem(数论)
11490 - Just Another Problem option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- 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 ...
- 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 ...
- CF45G Prime Problem 构造+数论
正解:构造+数论 解题报告: 传送门! maya这题好神仙啊我jio得,,,反正我当初听的时候是没有太懂的,,, 首先这题你要知道一些必要的数学姿势 比如哥德巴赫猜想巴拉巴拉的 然后直接讲题趴QAQ ...
随机推荐
- java 写一个"HelloJavaWorld你好世界"输出到操作系统文件Hello.txt文件中
package com.beiwo.homework; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...
- MySQL查询语句(select)详解(2)
7.子查询 当进行查询的时候,需要的条件是另外一个select语句的结果,这时候就要用到子查询 用于子查询的主要关键字有:in,not in,=,!=,exists,not exists等. 以下两张 ...
- Java 通过代理测试webService接口
以下代码针是针对电脑是通过代理上网的情况时使用: System.getProperties().put("http.proxyHost", "111.111.11.1&q ...
- 从DataGridView导出Excel
从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...
- 服务器中配置多个Tomcat及内存溢出配置
1.更改server.xml文件中端口(启动.关闭端口) 2.在startup.bat文件开头加上 SET JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0 ...
- Android课程---添加黑名单的练习2(课堂讲解)
实现黑名单的添加.修改.查询和删除,首先得有封装的3个类,便于使用 BlackNumber.java package com.hanqi.test3; /** * Created by Adminis ...
- C#_技巧:窗口抖动
原理 * 窗口抖动:即每隔一段很小的时间,窗口位置发生变化 * 时间控制:利用for循环||利用timer * 窗口位置发生变化:控件Left/Top属性或Location属性, 注:Left/To ...
- JavaScript:数组大全
栈/队列 数组es3: pop删除最后一项(栈) shift删除第一项(队列) push增加到最后(栈) unshift增加到最前(队列) reverse翻转 join转字符串 slice截取(切片) ...
- python学习道路(day5note)(列表生成式,生成器,装饰器,常用模块)
生成列表的方式 data = [1,2,3] 需求 每个数字加上1 # data = ( x*2 for x in range(5)) print(data) 列表生成式 后面的I赋予加1操 ...
- ECMAScript Web APIs node.js
https://hacks.mozilla.org/2015/04/es6-in-depth-an-introduction/ What falls under the scope of ECMASc ...