中国剩余定理+容斥_____Lucky7( hdu 5768 2016多校第四场 )

原创 2016年08月28日 16:47:48

Problem Description
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles when he faced a extremely difficult problem, and always solve it in seven minutes. 
?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.
 

Input
On the first line there is an integer T(T≤20) representing the number of test cases.
Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<1018) on a line where n is the number of pirmes. 
Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi. 
It is guranteed that all the pi are distinct and pi!=7. 
It is also guaranteed that p1*p2*…*pn<=1018 and 0<ai<pi<=105for every i∈(1…n).
 

Output
For each test case, first output "Case #x: ",x=1,2,3...., then output the correct answer on a line.
 

Sample Input
2 2 1 100 3 2 5 3 0 1 100
 

Sample Output
Case #1: 7 Case #2: 14
Hint
For Case 1: 7,21,42,49,70,84,91 are the seven numbers. For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.
 
题意:
给一个区间[L,R]和n对数ai,pi,问区间有多少个数m满足是7的倍数并且对于任意一对ai,pi都有m%pi != ai。所有pi都是相互互质。

分析:
因为一共最多15对数。那么最多2^15个状态。所以可以用容斥排除重复的部分。可以将7的倍数这一个条件也看做是同余方程组的其中一个。不过要提前判断一下,如果题目给你的pi中有7的倍数。那么判断这一对的ai,如果ai=0.显然无论如何这一对一定满足所以输出0。如果ai!=0显然无论这一对都不会满足。
注意的是在使用中国剩余定理的时候三个数相乘可能会超longlong所以需要使用快速乘法。

代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int k;
ll left,right,ans;
ll m[16],a[16],M[40000],s[40000];
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
ll gcd(ll a,ll b,ll &x,ll &y)
{
   ll t,ret;
   if(!b)
   {
     x = 1;
     y = 0;
     return a;
   }
   ret=gcd(b,a%b,x,y);
   t=x;
   x=y;
   y=t-a/b*y;
   return ret;
}
ll mul(ll a,ll b,ll mod)
{
    if(b < 0) return (-mul(a,-b,mod)%mod+mod);
    if(b == 0) return 0;
    if(b == 1) return a%mod;
    if(b%2 == 0) return mul((a+a)%mod,b/2,mod);
    return (mul((a+a)%mod,b/2,mod)+a)%mod;
}
ll lmes(ll a[],ll m[],int n,int num,ll& tm)
{
    ll ret = 0;
    tm = 7;
    for(int i = 0 ; i < n ; ++i){
        if(num&(1<<i))
        {
            tm*=m[i];
        }
    }
    for(int i = 0 ; i < n ; ++i)
    {
        if(!(num&(1<<i)))continue;
        ll mi = tm/m[i],x,y;
        gcd(mi,m[i],x,y);
        ret = (ret + mul(mul(a[i],x,tm),mi,tm))%tm;
    }
    if(ret<0) ret = (ret+tm);
    return ret;
}
int bitcount(int num)
{
    int c =0;
    for (c =0; num; ++c)
    {
        num &= (num -1) ; // 清除最低位的1
    }
    return c ;
}
void init()
{
    for(int i = 1 ; i < (1<<k) ; ++ i)
    {
        s[i] = lmes(a,m,k,i,M[i]);
    }
    ans = right/7 - left/7;
}
ll cnt(ll ta,ll tm)
{
    ll cnt1 = right/tm + (right%tm>=ta?1:0);
    ll cnt2 = left/tm + (left%tm>=ta?1:0);
  //  printf("%lld %lld %lld %lld\n",ta,tm,cnt1,cnt2);
    return cnt1 - cnt2;
}
void work()
{
    init();
    for(int i = 1 ; i < (1<<k) ; ++i)
    {
        if(bitcount(i)%2==0)
        {
            ans += cnt(s[i],M[i]);
        }
        else
        {
            ans -= cnt(s[i],M[i]);
        }
    }
    printf("%lld\n",ans);
}
int main()
{
    int t,_case=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld%lld",&k,&left,&right);
        left --;
        int tot = 0;
        bool flag = false;
        for(int i = 0 ; i < k ; i ++)
        {
            scanf("%lld%lld",&m[tot],&a[tot]);
            if(m[tot] % 7 == 0)
            {
                if(a[tot] == 0) flag = true;
                else tot --;
            }
            tot++;
        }
        k = tot;
        printf("Case #%d: ",++_case);
        if(flag)printf("0\n");
        else
            work();
    }
    return 0;
}






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

相关文章推荐

【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

题目链接:   http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意:   T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai...

HDU 5768 Lucky7(2016 Multi-University Training Contest 4 -1005)——中国剩余定理 + 容斥原理

[传送门](http://acm.hdu.edu.cn/showproblem.php?pid=5768)Lucky7Time Limit: 2000/1000 MS (Java/Others)   ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

HDU 5768 Lucky7 中国剩余定理+状压+容斥+快速乘法

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

hdu 5768 Lucky7 容斥原理 中国剩余定理

题目传送门:点击打开链接 题意:给你区间[a,b],在这区间里 找出 既是7的倍数 x,并且 x%pi!=ai的个数 有多少? 思路:看到这题  跟模有关系  大概跟 中国剩余定理有关,可是题目说的 ...

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

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 题意:求[a,b]区间内能被7整除且不满足x%pi=ai(1 题解: 因为满足任意一组...

hdu5768 Lucky7 中国剩余定理 + 容斥原理

/* 题目描述:给出n(0<=n<=15)对pi , ai ( 0 < ai < pi , 且p1*p2*...*pn < 1e18),问在x到y(0 < x < y < 1e18)的范围内有...

HDU5768 Lucky7[中国剩余定理+容斥定理]

E - Lucky7 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub...

HDU 5768 Lucky7 数论 中国剩余定理

原题见HDU 5768求[l,r]范围内是7的倍数,同时不满足任意一个给定的同余式的数的个数。如范围为[1,100],不满足模3余2或模5余3的7的倍数有7,21,42,49,70,84,91 ,故答...

[2016ACM多校] HDU5768 容斥原理 中国剩余定理

题意给出数组p和a,是7的倍数且除以任何一个pi余数不为ai的数是幸运数,问[x,y]中有多少幸运数。思路因为幸运数n要满足使得任意一个n%pi=ai不成立,鉴于中国剩余定理可以方便的解出{x%pi=...

HDU - 5768 容斥原理 + 中国剩余定理

题意: 给出一个范围[l,r]以及两个数组a,p,要求在这范围内mod 7 = 0,且mod p[i] != a[i]的数一共有多少个。 思路: 容斥原理,只考虑把a[0] = 0,p[0] =...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)