标签:
地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 754 Accepted Submission(s): 279
1 #include<cstdio> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 #include<stack> 7 #include<cstring> 8 #include<queue> 9 #include<set> 10 #include<string> 11 #include<map> 12 #define inf 9223372036854775807 13 #define INF 9e7+5 14 #define PI acos(-1) 15 using namespace std; 16 typedef long long ll; 17 typedef double db; 18 typedef long long LL; 19 const int maxn = 1e2 + 5; 20 const int mod = 1e9 + 7; 21 const db eps = 1e-9; 22 ll n, l, r, ai[maxn], pi[maxn]; 23 24 int Bitcount(ll x) { 25 return x ? Bitcount(x >> 1) + (x&1LL) : 0; 26 } //求当前状态选择的方程的个数 27 28 ll q_mul(ll a, ll b, ll mod) { 29 ll ret = 0; 30 while (b) { 31 if (b & 1) ret = (ret + a) % mod; 32 b >>= 1; 33 a = (a + a) % mod; 34 } 35 return ret; 36 } // 快速乘法 37 38 void Ex_Gcd(ll a, ll b, ll &x, ll &y){ 39 ll d; 40 if(b==0){ 41 x=1,y=0; 42 return; 43 } 44 Ex_Gcd(b,a%b,y,x); 45 y-=a/b*x; 46 } // 这里直接抄的标程的函数。。 47 48 ll CRT(ll p[], ll a[], ll cnt) { 49 ll M = 1; 50 ll res = 0; 51 for (int i = 0; i <= cnt; i++) M *= p[i]; 52 for (int i = 0; i <= cnt; i++) { 53 ll m = M / p[i], x, y; 54 Ex_Gcd(m, p[i], x, y); 55 res = (res + q_mul(q_mul(x, m, M), a[i], M)) % M; 56 } 57 return (res + M) % M; 58 } 59 60 ll solve(ll x) { 61 ll p[maxn], m[maxn], ans = 0; 62 p[0] = 7, m[0] = 0; 63 if (!x) return 0; 64 for (ll i = 1; i < (1LL << n); i++) { 65 ll num = Bitcount(i), t = 7, cnt = 0; 66 for (ll j = 0; j < n; j++) { 67 if (i & (1LL << j)) { 68 p[++cnt] = pi[j]; 69 m[cnt] = ai[j]; 70 t *= pi[j]; 71 } 72 } 73 ll res = CRT(p, m, cnt); 74 if (res > x) continue; 75 if (num & 1) ans -= (x - res) / t + 1; //容斥一下 76 else ans += (x - res) / t + 1; 77 } 78 return ans + x / 7; 79 } 80 81 int main() { 82 //cin.sync_with_stdio(false); 83 //freopen("tt.txt", "r", stdin); 84 //freopen("isharp.out", "w", stdout); 85 int t, cas = 1; 86 87 cin >> t; 88 while (t--) { 89 cin >> n >> l >> r; 90 for (int i = 0; i < n; i++) 91 cin >> pi[i] >> ai[i]; 92 printf("Case #%d: %I64d\n", cas++, solve(r) - solve(l-1)); 93 } 94 return 0; 95 }
HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
标签:
原文地址:http://www.cnblogs.com/ost-xg/p/5719899.html