HDU 1573 X问题 中国剩余定理
链接: pid=1573">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。输出零。
资料:http://yzmduncan.iteye.com/blog/1323599/
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#define PI acos(-1.0)
#define maxn 10005
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL MOD;
LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL r=extend_gcd(b,a%b,x,y);
LL t=x;
x=y;
y=t-a/b*y;
return r;
}
LL inv(LL a,LL m)
{
LL d,x,y;
d=extend_gcd(a,m,x,y);
if (d==1)
{
x=(x%m+m)%m;
return x;
}
else return -1;
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
bool merge(LL a1,LL m1,LL a2,LL m2,LL &a3,LL &m3)
{
LL d=gcd(m1,m2);
LL c=a2-a1;
if(c%d)
return false;
c=(c%m2+m2)%m2;
c/=d;
m1/=d;
m2/=d;
c*=inv(m1,m2);
c%=m2;
c*=m1*d;
c+=a1;
m3=m1*m2*d;
a3=(c%m3+m3)%m3;
return true;
}
LL CRT_next(LL a[],LL m[],int n)
{
LL a1=a[0],m1=m[0],a2,m2;
for(int i=1;i<n;i++)
{
LL aa,mm;
a2=a[i],m2=m[i];
if(!merge(a1,m1,a2,m2,aa,mm))
return -1;
a1=aa;
m1=mm;
}
MOD=m1;
LL aa=(a1%m1+m1)%m1;
if(aa==0)
aa+=m1;
return aa;
}
int main()
{
int T;
LL a[55],b[55];
scanf("%d",&T);
for(int ii=1; ii<=T; ii++)
{
int tot;
LL t1;
scanf("%I64d%d",&t1,&tot);
for(int i=0; i<tot; i++)
scanf("%I64d",&a[i]);
for(int i=0; i<tot; i++)
scanf("%I64d",&b[i]);
if(tot==1)
{
if(b[0]==0)
b[0]+=a[0];
if(t1<b[0])
printf("0\n");
else
printf("%I64d\n",(t1-b[0])/a[0]+1);
}
else
{
LL ans=CRT_next(b,a,tot);
if(ans==-1)
printf("0\n");
else if(ans>t1)
printf("0\n");
else
printf("%I64d\n",(t1-ans)/MOD+1);
}
}
return 0;
}
HDU 1573 X问题 中国剩余定理的更多相关文章
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- hdu 5446 Unknown Treasure 中国剩余定理+lucas
题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...
- HDU 3579 Hello Kiki 中国剩余定理(合并方程
题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...
- hdu 3579 Hello Kiki (中国剩余定理)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- HDU 1573 X问题(中国剩余定理标准解法)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 中国剩余定理 hdu 1573 X问题
HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 1573 X问题【扩展中国剩余定理】
扩展中国剩余定理的板子,合并完之后算一下范围内能取几个值即可(记得去掉0) #include<iostream> #include<cstdio> #include<cm ...
随机推荐
- HDU 3307 Description has only two Sentences
数学实在是差到不行了…… #include <cstdio> #include <cstring> #include <algorithm> #include &l ...
- AlertDialog弹出时背景明暗程度调整
今天有个需求是把弹出AlertDialog时的变暗的背景调整得不要那么暗. 一开始懒惰就直接百度中文搜索,结果找到的代码试了几次都不行. 后来老老实实开google.stackoverflow搜索,搜 ...
- Sightseeing Cows(最优比率环)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8915 Accepted: 3000 ...
- 集团财务分析BI项目中的财务系统环境
我国集团化经营模式起步较晚,集团管控模式及管控力度各异,集团范围内财务信息化水平及统一程度不尽相同,因此在实施集团财务分析一类的BI商业智能项目的过程中,在不同的集团之间遇到的财务系统及核算数据环境也 ...
- Sql语句之select 5种查询
select 5种子句:注意顺序where / group by /having / order by / limit / 清空表中的数据:truncate 表名: 导入表结构(不含数据): crea ...
- JavaSE学习总结第03天_Java基础语法2
03.01 数据类型中补充的几个小问题 1:在定义Long或者Float类型变量的时候,要加L或者f. 整数默认是int类型,浮点数默认是double. byte,short在定义的时候, ...
- hdu 4782 Beautiful Soupz
模拟.其实这题就是题目比较长而已...读完题目就差不多了.tag直接读就可以了,题目说了不用修改.然后整个题目就是让求text部分,严格按空格分开.注意每行前面空格个数. #include<al ...
- Zend Studio 如何配置本地apache服务器使用xdebug调试php脚本
本地环境搭配: apache 2.2 安装位置:D:/program files/Apache Software Foundation/Apache2.2 php 5.2.10 安装位置:C:/php ...
- 使用yum来下载RPM包而不进行安装
1. 安装yum-downloadonly. yum-utils 或 yum-plugin-downloadonly 软件包 (RHEL5) # yum install yum-downloadonl ...
- select_related
作用:减少DB访问次数 from django.db import models class Blog(models.Model): name = models.CharField(max_lengt ...