hdu 1573 同余定理

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
long long n,m;
long long int a[100];
long long int b[100];

int gcd(int a,int b){
	return b == 0? a: gcd(b,a%b);
}
int main(){
	int t;
	scanf("%d",&t);

	while(t--){
		scanf("%I64d%I64d",&n,&m);

		int ma = 1;

		for(int i = 0;i < m;i++){
			scanf("%d",&a[i]);
			ma = a[i]/gcd(ma,a[i])*ma;
		}
		for(int i = 0;i < m;i++){
			scanf("%d",&b[i]);
		}
		int x,c = 0;
		for(x = a[0]*c+b[0];x < ma && x <= n;c++,x += a[0]){
			int flag = 1;
			for(int i = 1 ;i < m;i++){
				if(x%a[i] != b[i]){
					flag = 0;
				}
			}
			if(flag){
				break;
			}
		}
		if(x>= ma || x > n){
			cout << "0" << endl;
		}
		else{
			int ans = (n-x)/ma;
			if(x % ma){
				ans ++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 10:13:05

hdu 1573 同余定理的相关文章

hdu 5690(同余定理找循环节 / 快速幂)

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define lld I64d 5 #ifdef _WIN32 6 #define LLD "%I64d" 7 #else 8 #define LLD "%lld" 9 #endif 10 const int N = 10000 + 100; 11 ll x,m,c,k; 12 int pos[N]; 1

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于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). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include

POJ 2769 Reduced ID Numbers 同余定理

Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8989 Accepted: 3610 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an

POJ--1465--Multiple【BFS+同余定理】

链接:http://poj.org/problem?id=1465 题意:给一个数字n,和m个数字,找一个由这些数字组成的最小的n的倍数,如果不存在输出0. 思路:这题怎么想都想不到bfs上去,看了别人的解题报告,其实是用bfs来枚举,但是加了一个牛逼的剪枝:同余.即如果A%X==B%X,则(A*10+K)%X==(B*10+K)%X. 我们枚举m中每一个数字做这个K,实际上是枚举了一个数,B*10是之前枚举的数字,如果这个数%X得到的值之前已经得到过,则没必要再往下计算,因为根据同余定理剩下的

2016湖南省赛----A 2016 (同余定理)

2016湖南省赛----A 2016 (同余定理) Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 组数据. 每组数据包含两个整数 n,m (1≤n,m≤10 9). Output 对于每组数据,输出一个整数表示满足条件的数量. Sample Input 32 63 2016 2016 1000000000 1000000000 Sample

Light oj 1214-Large Division (同余定理)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我就把A的每一位拆开,比如A是2341,那么2341=2000+300+40+1,然后你懂的... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

Codeforces 464C Substitutes in Number 同余定理+模拟

题目链接:点击打开链接 题意: 给定一串数字 下面有n个操作 每行格式形如 d->t d为一位数字,t为任意长度的数字. t的长度和不超过100000 问:最后的结果%1e9+7 思路: 首先我们可以得到一个结论: 同余定理使用后不能再修改数字. 那么为了让同余定理能够使用,我们倒序处理每个数字,这样就能保证能够使用同余定理. 记录每个数字实际代表的数字和实际对应的位数. 然后倒序处理上来即可. #include <stdio.h> #include <string.h> #

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

HDU 1573 CRT

CRT模板题 /** @Date : 2017-09-15 13:52:21 * @FileName: HDU 1573 CRT EXGCD.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h> #define LL long long #define PII pair