The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

传送门:https://nanti.jisuanke.com/t/41400

题意:

给你三个数组a,b,c,要你求有多少个三元组(i,j,k),使得
\[
\begin{array}{l}{\left|A_{i}-B_{j}\right| \leq C_{k}, \text { and }} \\ {\left|B_{j}-C_{k}\right| \leq A_{i}, \text { and }} \\ {\left|A_{i}-C_{k}\right| \leq B_{j}}\end{array}
\]

题解:

上面的不等式经过化简,我们可以得到

我们需要求有多少个三元组,使得\(A_i,B_j,C_k\)可以组成一个三角形

这样组成三角形的题目类似于HDU4609 (https://www.cnblogs.com/buerdepepeqi/p/11236100.html)

但是不同的是 我们需要从三个数组中选择

所以这里就涉及到了选择重复的问题,我们考虑去重

假设拿a+b做一遍卷积,得到长度为a+b的木棍的数量,

我们假设 c是三角形的最长边,那么a,b,c三根木棍不能组成三角形的情况就是c的长度大于a+b的数量

我们枚举(a+b)这个长度,那么不能组成三角形的数量就是 可以组成当前长度的(a,b)的方案数*大于这个长度的c的数量

所以按照这样来说 我们就可以得到 做三遍卷积,分别枚举c为最长边时,b为最长边时, a为最长边时 不和法的数量,然后用所有的三元组的数量减去不合法的三元组的数量就是合法的三元组的数量

这里有一个小技巧,就是小范围暴力??如果小范围不暴力你就会T

(留恋一下机房惨案

我们来分析一下为什么?

我们假设FFT的Complex类带了一个常数 x

在T是100的情况下,我们单纯的跑三遍FFT,那么就是三遍正的FFT,三遍IDFT,那么就是6的一个常数

复杂度最后就是

\[
T*2^{log(2n)}*log(2n)*6*x\=100*2^{18}*18*6=2,831,155,200?
\]
然而暴力的复杂度是n2

假设有20组大数据 80组小数据(1000)

那么复杂度就是
\[
20*18*2^{18}*6+1000*1000*80=646,231,040
\]
所以 小范围暴力 大范围FFT的复杂度是比较优秀滴

(感谢NE大佬对于复杂度的分析

代码:

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  //                    /  _||||| -:- |||||-  //                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 5e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
    return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
    double ans = 1.0;
    while(b) {
        if(b % 2)ans = ans * a;
        a = a * a;
        b /= 2;
    } return ans;
}
LL quick_pow(LL x, LL y) {
    LL ans = 1;
    while(y) {
        if(y & 1) {
            ans = ans * x % mod;
        } x = x * x % mod;
        y >>= 1;
    } return ans;
}

LL res[maxn << 2], len;
struct Complex {
    double r, i;

    Complex(double r = 0, double i = 0) : r(r), i(i) {};

    Complex operator+(const Complex &rhs) {
        return Complex(r + rhs.r, i + rhs.i);
    }

    Complex operator-(const Complex &rhs) {
        return Complex(r - rhs.r, i - rhs.i);
    }

    Complex operator*(const Complex &rhs) {
        return Complex(r * rhs.r - i * rhs.i, i * rhs.r + r * rhs.i);
    }
} va[maxn << 2], vb[maxn << 2];

void rader(Complex F[], int len) { //len = 2^M,reverse F[i] with  F[j] j为i二进制反转
    int j = len >> 1;
    for (int i = 1; i < len - 1; ++i) {
        if (i < j) swap(F[i], F[j]);  // reverse
        int k = len >> 1;
        while (j >= k) {
            j -= k;
            k >>= 1;
        }
        if (j < k) j += k;
    }
}

void FFT(Complex F[], const int &len, const int &t) {
    rader(F, len);
    for (int h = 2; h <= len; h <<= 1) {
        Complex wn(cos(-t * 2 * Pi / h), sin(-t * 2 * Pi / h));
        for (int j = 0; j < len; j += h) {
            Complex E(1, 0); //旋转因子
            for (int k = j; k < j + h / 2; ++k) {
                Complex u = F[k];
                Complex v = E * F[k + h / 2];
                F[k] = u + v;
                F[k + h / 2] = u - v;
                E = E * wn;
            }
        }
    }
    if (t == -1)   //IDFT
        for (int i = 0; i < len; ++i)
            F[i].r /= len;
}

void Conv(Complex a[], Complex b[], const int &len) { //求卷积
    FFT(a, len, 1);
    FFT(b, len, 1);
    for (int i = 0; i < len; ++i) a[i] = a[i] * b[i];
    FFT(a, len, -1);
}

void work() {
    Conv(va, vb, len);
    for (int i = 0; i < len; ++i)res[i] = va[i].r + 0.5;
}
int a[maxn], b[maxn], c[maxn];
int numa[maxn], numb[maxn], numc[maxn];
int suma[maxn], sumb[maxn], sumc[maxn];//长度为i的数量
LL resab[maxn], resbc[maxn], resac[maxn];//长度为i的 a,b的个数
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int T;
    int cas = 1;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        int maxlena = 0, maxlenb = 0, maxlenc = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            numa[a[i]]++;
            maxlena = max(maxlena, a[i]);
        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &b[i]);
            numb[b[i]]++;
            maxlenb = max(maxlenb, b[i]);

        }
        for(int i = 1; i <= n; i++) {
            scanf("%d", &c[i]);
            numc[c[i]]++;
            maxlenc = max(maxlenc, c[i]);
        }
        LL ans = 1ll * n * n * n;
        if(n <= 1000) {
            for(int i = 1; i <= maxlena; i++) {
                suma[i] = suma[i - 1] + numa[i];
            }
            for(int i = 1; i <= maxlenb; i++) {
                sumb[i] = sumb[i - 1] + numb[i];
            }
            for(int i = 1; i <= maxlenc; i++) {
                sumc[i] = sumc[i - 1] + numc[i];
            }
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= n; j++) {
                    resab[a[i] + b[j]]++;
                    resbc[b[i] + c[j]]++;
                    resac[a[i] + c[j]]++;
                }
            }
            int mx = max(maxlena, max(maxlenb, maxlenc));
            for(int i = 1; i <= mx; i++) {
                if(i <= maxlenc) ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
                if(i <= maxlenb) ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
                if(i <= maxlena) ans -= resbc[i] * (suma[maxlena] - suma[i]);
            }
            memset(suma, 0, sizeof(int) * (maxlena + 2));
            memset(sumb, 0, sizeof(int) * (maxlenb + 2));
            memset(sumc, 0, sizeof(int) * (maxlenc + 2));
            memset(numa, 0, sizeof(int) * (maxlena + 2));
            memset(numb, 0, sizeof(int) * (maxlenb + 2));
            memset(numc, 0, sizeof(int) * (maxlenc + 2));
            memset(resac, 0, sizeof(LL) * (mx * 2 + 2));
            memset(resab, 0, sizeof(LL) * (mx * 2 + 2));
            memset(resbc, 0, sizeof(LL) * (mx * 2 + 2));
        } else {
            maxlena++, maxlenb++, maxlenc++;
            len = 1;
            int mxab = max(maxlena, maxlenb);
            while(len < 2 * mxab) len <<= 1;
            for(int i = 0; i < len; i++) {
                if (i < mxab) {
                    va[i] = Complex(numa[i], 0);
                    vb[i] = Complex(numb[i], 0);
                } else
                    va[i] = vb[i] = Complex(0, 0);
            }
            work();
            for (int i = 0; i < len; i++) resab[i] = res[i];

            len = 1;
            int mxac = max(maxlena, maxlenc);
            while(len < 2 * mxac) len <<= 1;
            for(int i = 0; i < len; i++) {
                if (i < mxac) {
                    va[i] = Complex(numa[i], 0);
                    vb[i] = Complex(numc[i], 0);
                } else
                    va[i] = vb[i] = Complex(0, 0);
            }
            work();
            for (int i = 0; i < len; i++) resac[i] = res[i];

            len = 1;
            int mxbc = max(maxlenb, maxlenc);
            while(len < 2 * mxbc) len <<= 1;
            for(int i = 0; i < len; i++) {
                if (i < mxbc) {
                    va[i] = Complex(numb[i], 0);
                    vb[i] = Complex(numc[i], 0);
                } else
                    va[i] = vb[i] = Complex(0, 0);
            }
            work();
            for (int i = 0; i < len; i++) resbc[i] = res[i];

            for(int i = 1; i <= maxlena; i++) {
                suma[i] = suma[i - 1] + numa[i];
            }
            for(int i = 1; i <= maxlenb; i++) {
                sumb[i] = sumb[i - 1] + numb[i];
            }
            for(int i = 1; i <= maxlenc; i++) {
                sumc[i] = sumc[i - 1] + numc[i];
            }

            for (int i = 1; i <= 2 * mxab; ++i) {
                if (i > maxlenc) break;
                ans -= resab[i] * (sumc[maxlenc] - sumc[i]);
            }
            for (int i = 1; i <= 2 * mxbc; ++i) {
                if (i > maxlena) break;
                ans -= resbc[i] * (suma[maxlena] - suma[i]);
            }
            for (int i = 1; i <= 2 * mxac; ++i) {
                if (i > maxlenb) break;
                ans -= resac[i] * (sumb[maxlenb] - sumb[i]);
            }
            memset(suma, 0, sizeof(int) * (maxlena + 2));
            memset(sumb, 0, sizeof(int) * (maxlenb + 2));
            memset(sumc, 0, sizeof(int) * (maxlenc + 2));
            memset(numa, 0, sizeof(int) * (maxlena + 2));
            memset(numb, 0, sizeof(int) * (maxlenb + 2));
            memset(numc, 0, sizeof(int) * (maxlenc + 2));
            memset(resac, 0, sizeof(LL) * (mxac * 2 + 2));
            memset(resab, 0, sizeof(LL) * (mxab * 2 + 2));
            memset(resbc, 0, sizeof(LL) * (mxbc * 2 + 2));
        }
        printf("Case #%d: %lld\n", cas++, ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/buerdepepeqi/p/11525304.html

时间: 2024-10-15 08:30:46

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)的相关文章

The Preliminary Contest for ICPC Asia Shanghai 2019

D. Counting Sequences I 暴力搜索. #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1000000007; map<vector<short>, short> m; vector<short> vec; void calc(int num1) { vector<short> tmp; if(num1) t

Digit sum-----The Preliminary Contest for ICPC Asia Shanghai 2019

A digit sum S_b(n)Sb?(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 + 3 = 8S10?(233)=2+3+3=8, S_{2}(8)=1 + 0 + 0 = 1S2?(8)=1+0+0=1, S_{2}(7)=1 + 1 + 1 = 3S2?(7)=1+1+1=3. Given NN and bb, you need to calculate \sum_{n=1}^{N} S_b

The Preliminary Contest for ICPC Asia Shanghai 2019 G. Substring (滑窗+哈希)

G. Substring 哎 一直超时or超内存 然后一直改一直改 然后 是 答案错误 然后 然后 最后结论是哈希姿势不对 我在别的地方找了这个: //https://www.cnblogs.com/napoleon_liu/archive/2010/12/29/1920839.html uint32_t hash( uint32_t a) { a = (a+0x7ed55d16) + (a<<12); a = (a^0xc761c23c) ^ (a>>19); a = (a+0x

01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019

题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: 从最大重量的石子开始背包,每次ans+=dp[j-v[i]]就行了. 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include &

给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019

题意:https://nanti.jisuanke.com/t/41422 对每一位进行找循环节规律就行了. 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <io

The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum

题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; int dp[11][1000001]={0}; int main() { for(int i=2;i<=10;i++) { for(int j=1;j<=1000000;j++) { int t=j; int res=0; res=j%i; dp[i][j]=res+dp[i][j/i]+dp[i][j

The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs

题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using namespace std; map<int,int>mp; int main() { int T; scanf("%d",&T); int n,m; int l,r; for(int i=1;i<=T;i++) { mp.clear(); scanf(&quo

The Preliminary Contest for ICPC Asia Shanghai 2019 D. Counting Sequences I

题目:https://nanti.jisuanke.com/t/41412思路:dfs           先取ai>2  2^12>3000 因此至多取11个 其余用1补           (3000*2)-(3000+2)=2998 那么需要加入2998个1 正好3000位 所以 3000是ai最大取值           计算ans时 有重复元素的排列组合 :如1112233 res=7!/(3!*2!*2!)           另外预处理阶乘及其逆元 #include<bit

The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game

题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum(S−S′))恒成立            先算01背包方案数 再从小到大排序进行退背包 #include<bits/stdc++.h> using namespace std; const int mod=1e9+7; int a[400]; int dp[150001]; int main()