题目链接:Lucky7

题意:求在l和r范围内,满足能被7整除,而且不满足任意一组,x mod p[i] = a[i]的数的个数。

思路:容斥定理+中国剩余定理+快速乘法。 (奇+ 偶-)

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; #define LL long long
#define FOR(i, n) for (int i=0; i<n; ++i)
LL l, r; int extend_gcd(LL a, LL b, LL &x, LL &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int r = extend_gcd(b, a%b, y, x);
y -= x*(a/b);
return r;
}
} LL qMul(LL a, LL b, LL mod) {
a %= mod;
LL ret = 0;
while(b) {
if (b & 1) ret = (ret + a) % mod;
b >>= 1;
a = (a + a) % mod;
}
return ret;
} LL CRT(LL *a, LL *m, int n) {
LL M = 1;
for (int i=0; i<n; ++i) M *= m[i]; LL x = 0;
LL d, y;
for (int i=0; i<n; ++i) {
//LL d, y;
LL tm = M/m[i];
extend_gcd(m[i], tm, d, y);
x = (x + qMul( qMul(d, tm, M), a[i], M)) % M; // 参数传递有顺序
}
x = (x + M) % M;
return (r+M-x)/M - (l-1+M-x)/M; // 直接返回l r区间有多少个解。
} LL a[20], m[20];
LL chu[20], rema[20]; int main() {
freopen("1.in.cpp", "r", stdin);
int t;
scanf("%d", &t);
int cas = 0; while(t--) {
int n;
scanf("%d%lld%lld", &n, &l, &r);
FOR(i, n) {
scanf("%lld%lld", &chu[i], &rema[i]);
}
LL tot = (1<<n);
LL ans = r/7 - (l-1)/7; for (int i=1; i<tot; ++i) {
//cout << i << "++++\n";
int cnt = 0;
FOR (j, n) {
if (i&(1<<j)) {
m[cnt] = chu[j];
a[cnt] = rema[j];
cnt++;
}
}
m[cnt] = 7, a[cnt] = 0;
cnt++;
LL tans = CRT(a, m, cnt);
cnt = (cnt&1) ? 1 : -1;
ans += tans * cnt;
}
//cout << "++++\n";
printf("Case #%d: %I64d\n", ++cas, ans);
}
return 0;
}

HDU 5768 中国剩余定理的更多相关文章

  1. hdu 2891 中国剩余定理

    从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...

  2. hdu 3579 Hello Kiki 不互质的中国剩余定理

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

  3. 《孙子算经》之"物不知数"题:中国剩余定理

    1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数.  如果 m ...

  4. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  5. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...

  6. poj1006中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103506   Accepted: 31995 Des ...

  7. (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)

    前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...

  8. 洛谷P2480 [SDOI2010]古代猪文(费马小定理,卢卡斯定理,中国剩余定理,线性筛)

    洛谷题目传送门 蒟蒻惊叹于一道小小的数论题竟能涉及这么多知识点!不过,掌握了这些知识点,拿下这道题也并非难事. 题意一行就能写下来: 给定\(N,G\),求\(G^{\sum \limits _{d| ...

  9. 洛谷P3868 [TJOI2009]猜数字(中国剩余定理,扩展欧几里德)

    洛谷题目传送门 90分WA第二个点的看过来! 简要介绍一下中国剩余定理 中国剩余定理,就是用来求解这样的问题: 假定以下出现数都是自然数,对于一个线性同余方程组(其中\(\forall i,j\in[ ...

随机推荐

  1. 关于Ubuntu中passwd、shadow、group等文件

    在Ubuntu系统中,/etc目录下,有三个文件:passwd shadow group,可能我们已经在用了,但是没有注意到其详细. 这三个配置文件用于系统帐号管理,都是文本文件,可用vi等文本编辑器 ...

  2. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  3. JAVA中在Myeclipse里把表导入成相应的poco实体类

    参考:地址: http://blog.csdn.net/jintaiyong/article/details/7383982

  4. Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace

    Oracle正则表达式函数:regexp_like.regexp_substr.regexp_instr.regexp_replace   --去掉所有特殊字符,只剩字母  SELECT REGEXP ...

  5. boost线程的问题:

    可以看看这里:http://blog.csdn.net/misskissc/article/details/9859753 我的总结: 1,用thread类来创建一个线程,它的构结函数 : (1)th ...

  6. linux 跨IP拷贝命令 scp

    原文:http://blog.csdn.net/mexican_jacky/article/details/52847094 scp -r ROOT/ tms2api@10.230.4.215:/Ja ...

  7. hiho1091_clicker背包问题

    问题 类似有限背包问题,题目链接:clicker 实现 #include<stdio.h> #include<cmath> #include<iostream> # ...

  8. 动画Animation

    动画分类:Animation 单一动画 AnimationSet 复合动画 AnimationSet是Animation的实现子类,Animation是一个抽象类,他的实现子类主要有如下几种: 主要有 ...

  9. xcode 真机调试 failed to get the task for process xxx

    xcode 真机调试 failed to get the task for process xxx 此错误原因是,使用 in house profile 签名了真机调试的证书: 在 target--- ...

  10. 八大排序算法的 Python 实现

    转载: 八大排序算法的 Python 实现 本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 描述 插入排序的基本操作就是将一个 ...