hdu 5768 LUCKY 7(数论...)

Lucky7
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1300 Accepted Submission(s): 492

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.

Author
FZU

Source
2016 Multi-University Training Contest 4

怎么说呢,这道题。考察的东西就是 中国剩余定理 和 容斥原理。
直接上AC代码。不太懂的看这里–数论知识的合集

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define MEM(a,x) memset(a,x,sizeof(a))
typedef unsigned long long ull;
using namespace std;
#define LL long long
const int N = 100000 + 100;
LL a[N], m[N];
int s[N], n;
// 扩展欧几里得算法(求最大公约数)
LL extend_Euclid(LL a, LL b, LL &x, LL &y)
{
    if(b==0)
    {
        x = 1;
        y = 0;
        return a;
    }
    LL r = extend_Euclid(b, a%b, y, x);
    y -= a/b*x;
    return r;
}
LL gao(LL x, LL r, LL p)
{
    return (x-r)/p;
}
//快速幂
LL mult(LL a, LL k, LL m)
{
    LL res = 0;
    while(k)
    {
        if(k & 1)
            res = (res + a) % m;
        k >>= 1;
        a = (a << 1) % m;
    }
    return res;
}
//中国剩余定理
LL China(LL l, LL r)
{
    LL M = 1, ans = 0;
    for (int i = 0; i <= n; ++i) if(s[i])
    {
        M *= m[i];
    }
    for(int i = 0; i <= n; i++) if(s[i])
    {
        LL Nn = M/m[i];
        LL x, y;
        extend_Euclid(Nn, m[i], x, y);
        x = (x%m[i] + m[i]) % m[i];
        ans = ((ans+mult(a[i]*Nn%M, x, M))%M + M) % M;
    }
    LL ret = gao(r+M, ans, M) - gao(l-1+M, ans, M);
    return ret;
}
int main()
{
//    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T, o = 0;
    scanf("%d", &T);
    while(T--)
    {
        LL l, r;
        scanf("%d%lld%lld", &n, &l, &r);
        memset(s, 0, sizeof(s));
        m[n] = 7;
        a[n] = 0;
        s[n] = 1;
        for(int i = 0; i < n; i++)
            scanf("%lld%lld", &m[i], &a[i]);
        LL ans = 0;
        int all = 1 << n;
        for(int i = 0; i < all; i++)
        {
            int t = i, k = 0;
            for(int j = 0; j < n; j++)
            {
                s[j] = t & 1;
                t >>= 1;
                k += s[j];
            }
            k = k & 1 ? -1 : 1;
            ans += 1 * k * China(l, r);
        }
        printf("Case #%d: %lld\n", ++o, ans);
    }
    return 0;
}

啦啦啦 美好的一天呀!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值