1. 程式人生 > >Codeforces 819 C. Mister B and Beacons on Field 容斥 數學

Codeforces 819 C. Mister B and Beacons on Field 容斥 數學

題意:

有一個座標系,有兩個點a,b座標分別(n,0),(0,m),還有一個任意點c

問題:
  • a向原點移動,詢問移動過程中有多少位置滿足三點構成的三角形的面積等於S
  • a在原點,b向原點移動,詢問移動過程中有多少位置滿足三點構成的三角形的面積等於S
演算法:
對於第一問:
  • 這裡寫圖片描述
  • 設點c座標為(x,y)
  • 可以列出一個等式:S=xn2+yi2ni22S=xn+yinixn+yi=2S+ni
  • 那麼上式(x,y)合法的必要條件為:gcd(n,i)|2S+nigcd(n,i)|2S
  • 對應的就是就是求有多少個i[
    1,m]
    滿足gcd(n,i)|2S
  • 對於這個問題將n,i,2S分解質因數,得到:n=j=1ωpAjji=j=1ωpBjj2S=j=1ωpCjj
  • 可以將問題轉化為:min(Ai,Bi)Ci
  • 由於i是不確定的,一個很簡單的想法就是去容斥Bi
  • 實現這個的效率就是O(2ω+ω)
對於第二問:
  • 這裡寫圖片描述
  • 設點c座標為(x,y)
  • 可以列出一個等式:S=ix22S=ix
  • 因為c是整數點所以c合法的必要條件就是i|2S
  • 直接列舉約束計算就好…..
  • 效率O(σ0)
程式碼:
#include <cstdio>
#include <string.h>
#include <algorithm> using namespace std; int rd() { int x = 0; char c = getchar(); while (c > '9' || c < '0') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return x; } void wt(long long x) { if (x >= 10) wt(x / 10); putchar
(x % 10 + 48); } const int N = 3e6 + 10; struct num { int p, q; bool operator < (const num &t) const { return p < t.p; } }; struct Fac { num k[20]; int tot; void clear() { for (int i = 0; i < 20; i ++) k[i].q = k[i].p = 0; tot = 0; } void sort() { ::sort(k + 1, k + tot + 1); } }A, B, C; int pri[N], mf[N], n1, n2, n3, s1, s2, s3, m1, m2, m3; long long n, m, s, ans; void getp() { int n = 3e6; for (int i = 2; i <= n; i ++) { if (!mf[i]) mf[i] = i, pri[++pri[0]] = i; for (int j = 1; j <= pri[0] && i * pri[j] <= n; j ++) { mf[i * pri[j]] = pri[j]; if (i % pri[j] == 0) break; } } } int id[N]; void facor(Fac &a, int x) { for(; mf[x]; x /= mf[x]) { if (!id[mf[x]]) id[mf[x]] = ++ a.tot, a.k[a.tot].p = mf[x]; a.k[id[mf[x]]].q ++; } } void dfs(int u, long long sum) { if (sum > n) return; if (u > C.tot) { ans ++; return; } dfs(u + 1, sum); for (int i = 1; i <= C.k[u].q; i ++) sum *= C.k[u].p, dfs(u + 1, sum); } void dfs2(int x, long long y, int z) { if (!y) return; if (x > B.tot) { ans += y * z; return;} dfs2(x + 1, y, z); for (int i = 0; i <= B.k[x].q; i ++) y /= B.k[x].p; dfs2(x + 1, y, -z); } int main() { getp(); for (int T = rd(); T; T --) { ans = 0, A.clear(), B.clear(), C.clear(); n1 = rd(), n2 = rd(), n3 = rd(), n = 1ll * n1 * n2 * n3; m1 = rd(), m2 = rd(), m3 = rd(), m = 1ll * m1 * m2 * m3; s1 = rd(), s2 = rd(), s3 = rd(), s = 1ll * s1 * s2 * s3; memset(id, 0, sizeof id); facor(A, n1), facor(A, n2), facor(A, n3); memset(id, 0, sizeof id); facor(C, 2 * s1), facor(C, s2), facor(C, s3); A.sort(), C.sort(); dfs(1, 1); int i = 1, j = 1; while (i <= A.tot && j <= C.tot) { if (A.k[i].p == C.k[j].p) { if (A.k[i].q > C.k[j].q) B.k[++B.tot].p = C.k[j].p, B.k[B.tot].q = C.k[j].q; i ++, j ++; continue; } if (A.k[i].p < C.k[j].p) { B.k[++B.tot].p = A.k[i].p, B.k[B.tot].q = 0; i ++; continue; } j ++; } for (; i <= A.tot; i ++) B.k[++B.tot].p = A.k[i].p, B.k[B.tot].q = 0; dfs2(1, m, 1); wt(ans), putchar(10); } return 0; }

相關推薦

Codeforces 819 C. Mister B and Beacons on Field 數學

題意: 有一個座標系,有兩個點a,b座標分別(n,0),(0,m),還有一個任意點c。 問題: a向原點移動,詢問移動過程中有多少位置滿足三點構成的三角形的面積等於S。 a在原點,b向原點移動,詢問移動過程中有多少位置滿足三

Codeforces 819 D. Mister B and Astronomers 數論

題意: 有T個石子,n個人,每個人輪流取石子,一顆石子只能取一次,求每個人能取到多少石子。 假設i-1號取的位置為pos,那麼i號取的位置就是(pos+ai)modT,如果當前位置的石子已經被取走,這個人的所取石子數量不增加,i+1取的位置為(po

Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts(瞎搞)

time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Some time ago Mister

Codeforces 914 C. Travelling Salesman and Special Numbers (dp)

Description The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform

codeforces 768 C Jon Snow and his Favourite Number(迴圈節)

題意: 有一個長度n的數列,瓊恩有一個喜愛的數x,瓊恩每次去隔一個 數對數列裡的數異或,請問k次操作後數列裡最大的數和最小的數分別是什麼 解題思路: 這種題一般來說操作後的數列是有迴圈節的,然後看到群裡qc爸爸問有沒有迴圈節不是2的例子的時候就更確定了。先去模擬下操作,然

Codeforces#458 C.Travelling Salesman and Special Numbers

題目: http://codeforces.com/contest/914/problem/C 分析: 一次操作後,必定變為1000以內的數; 則先暴力處理出1000以內的合法數; 然後遞推出

Codeforces 483B - Friends and Presents(二分+

con pan const c++ logs () get com end 483B - Friends and Presents 思路:這個博客寫的不錯:http://www.cnblogs.com/windysai/p/4058235.html 代碼: #includ

CodeForces - 451E Devu and Flowers【簡單

題意:給N個數(a1,a2……an)和一個S,求(b1+b2+……+bn)==S的方案數,其中0<=bi<=ai; 分析:題意很簡單,考慮容斥。          其中U為全集,t為U的i元組。 由於N<=20,直接

Codeforces 838A - Binary Blocks(二維前綴和+)

using min class blank -i push_back cin size 1的個數 838A - Binary Blocks 思路:求一下前綴和,然後就能很快算出每一小正方塊中1的個數了,0的個數等於k*k減去1的個數,兩個的最小值就是要加進答案的值。 代碼:

Devu and Flowers lucas定理+原理

原理 容斥原理 title pac cond rst like with lld Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box con

Codeforces483B. Friends and Presents(二分+原理)

題目連結:傳送門 題目: B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input standard input output standard out

ACM-ICPC 2018 焦作賽區網路預賽 H String and Times (字尾陣列+)

Now you have a string consists of uppercase letters, two integers A and B. We call a substring wonderful substring when the times it appea

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】

section wal cstring describe test mathjax tar include max In the year of 30XX30XX participants of some world programming champi

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt

seconds ack ble pseudo lose tinc += repeat ac代碼 B. T-Shirt Hunt time limit per test2 seconds memory limit per test256 megabytes inputsta

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

initial index 技術 ble continue efi whole ret rem Pronlem A In a small restaurant there are a tables for one person and b tables for t

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) B. Black Square

con style its als name lac 思路 amp include 題意:問是否可以形成一個全黑正方形 思路:可以找出正方形的邊,然後判斷下這個矩陣是否容得下,n,m都比邊短,比賽的時候寫麻煩了,還去找了這個正方形究竟在哪個位置,這樣的話得考慮很多情況,不如

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction

題意 一個 () max 字典序 log class its 位置 題意:給出各個字符串出現的起始位置,問整個的字符串是什麽,(字典序最小) 思路:開始寫的是用set+優先隊列存取每個位置出現的最長字符串,然後遍歷,爆內存。。。爆。。。內。。。存。。。我們可以用並查集,已經

Codeforces C - Om Nom and Candies

end pro name color ron cin urn 復雜度 define C - Om Nom and Candies 思路:貪心+思維(或者叫數學)。假設最大值max(wr,wb)為wr,當c/wr小於√c時,可以枚舉r糖的數量(從0到c/wr),更新答案,復雜

codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry

pre sig signed 時間復雜度 struct 得到 ast 16px etc             C. Sasha and Array 題目大意&題目鏈接:   http://codeforces.com/problemset/problem/71

PAT1065: A+B and C (64bit)

from whether ble 思路 限制 eth test mco ont 1065. A+B and C (64bit) (20) 時間限制 100 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 H