http://acm.hdu.edu.cn/showproblem.php?pid=1573

X问题

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4439    Accepted Submission(s): 1435

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
 
这道题除数不是两两互质的,也就不能直接套用中国剩余定理模板,既然不能直接套用就两两合成
 
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. const int M = ;
  9. typedef long long ll;
  10. int r;
  11. ll n[M], b[M], N;
  12.  
  13. void gcd(ll a, ll b, ll &x, ll &y)
  14. {
  15. if(b == )
  16. {
  17. x = ;
  18. y = ;
  19. r = a;
  20. return ;
  21. }
  22. gcd(b, a % b, x, y);
  23. ll t = x;
  24. x = y;
  25. y = t - a / b * y;
  26. }
  27. ll CRT2(ll b[], ll n[], int m)
  28. {
  29. int f = ;
  30. ll n1 = n[], n2, b1 = b[], b2, c, t, k, x, y;
  31. for(int i = ; i < m ; i++)
  32. {
  33. n2 = n[i];
  34. b2 = b[i];
  35. c = b2 - b1;
  36. gcd(n1, n2, x, y);
  37. if(c % r != )
  38. {
  39. f = ;
  40. break;
  41. }
  42. k = c / r * x;
  43. t = n2 / r;
  44. k = (k % t + t) % t;
  45. b1 = b1 + n1 * k;
  46. n1 = n1 * t;
  47. }
  48. if(f)//无解
  49. return ;
  50. if(b1 == )
  51. b1 = n1;
  52. if(b1 > N)
  53. return ;
  54. return (N - b1) / n1 + ;
  55. }
  56.  
  57. int main()
  58. {
  59. int t, m;
  60. scanf("%d", &t);
  61. while(t--)
  62. {
  63. scanf("%lld%d", &N, &m);
  64. for(int i = ; i < m ; i++)
  65. scanf("%lld", &n[i]);
  66. for(int i = ; i < m ; i++)
  67. scanf("%lld", &b[i]);
  68. printf("%lld\n", CRT2(b, n, m));
  69. }
  70. return ;
  71. }
 

hdu X问题 (中国剩余定理不互质)的更多相关文章

  1. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

  2. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  3. X问题(中国剩余定理+不互质版应用)hdu1573

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. Hello Kiki(中国剩余定理——不互质的情况)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  6. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  7. 中国剩余定理模数互质的情况模板(poj1006

    http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...

  8. hdu 5768 Lucky7 中国剩余定理+容斥+快速乘

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  9. HDU 5768 Lucky7 (中国剩余定理+容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...

随机推荐

  1. django-DIL模板自定义过滤器,自定义标签,自定义包含标签

    自定义过滤器 DTL模板语言生来只是为了方便的展示信息,所以与编程语言相比显得有点薄弱,有时候不能满足我们的需求.因此django提供了一个接口,让开发者能自定义标签和过滤器. 首先,你需要添加一个t ...

  2. 熟悉下apple 马甲包

    一.什么是马甲包 马甲包是利用App store 规则漏洞,通过技术手段,多次上架同一款产品的方法.马甲包和主产品包拥有同样的内容和功能,除了icon和应用名称不能完全一致,其他基本一致. 二.为什么 ...

  3. C入门程序整体框架图

    0.1:概述, 从头开始介绍一门编程语言总是显得很困难,因为有许多的细节还没有介绍,很难让读者在大脑中形成一幅完整的图, 所以起步时以一个列程序向学折介绍大体的C,试图使大家对C有一个整体大概 影响. ...

  4. spinnaker

    https://www.spinnaker.io/guides/tutorials/codelabs/kubernetes-source-to-prod/#configuring-kubernetes

  5. Excel VBA入门(八)单元格边框

    本文基于以下文件 http://pan.baidu.com/s/1nvJtsu9 (部分)内容预览: 1. 边框样式 Sub cell_format() Dim sht As Worksheet Di ...

  6. 安装 ORACLE 11G出现Error Message:PRVF-7535

    Error Message:PRVF-7535 : Proper architectureis not found on node "tsing" [Expected = &quo ...

  7. 9.Palindrome Number (INT)

    Determine whether an integer is a palindrome. Do this without extra space. class Solution { public: ...

  8. Jenkins 邮箱配置及问题解决

    Failed to send out e-mail javax.mail.MessagingException: Could not connect to SMTP host: smtp.rytong ...

  9. spring注解@Value取不到值【转】

    spring注解@Value取不到值 今天在一个项目中发现一个情况,在Service中取不到name值,直接输出了{name}字符串,找了好久,最后在一篇文章中找到解决方案. 解决这个问题的一篇文章( ...

  10. SQL编程:group by合并结果字符串 ---> group_concat函数就能行

    1.表结构 create table tt(id int,v varchar(30));              insert into tt values(1,'a'),(1,'b'),(2,'b ...