XJTUOJ wmq的A×B Problem FFT/NTT
wmq的A×B Problem
发布时间: 2017年4月9日 17:06 最后更新: 2017年4月9日 17:07 时间限制: 3000ms 内存限制: 512M
这是一个非常简单的问题。
wmq如今开始学习乘法了!他为了训练自己的乘法计算能力,写出了n个整数,并且对每两个数a,b都求出了它们的乘积a×b。现在他想知道,在求出的n(n−1)2个乘积中,除以给定的质数m余数为k(0≤k<m)的有多少个。
第一行为测试数据的组数。
对于每组测试数据,第一行为2个正整数n,m,2≤n,m≤60000,分别表示整数的个数以及除数。
接下来一行有n个整数,满足0≤ai≤109。
保证总输出行数∑m≤3×105。
对每组数据输出m行,其中第i行为除以m余数为(i−1)的有多少个。
2
4 5
2 0 1 7
4 2
2 0 1 6
3
0
2
0
1
6
0
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = +, M = 1e3+,inf = 2e9,mod = ; int MOD;
inline int mul(int a, int b){
return (long long)a * b % MOD;
}
int power(int a, int b){
int ret = ;
for (int t = a; b; b >>= ){
if (b & )ret = mul(ret, t);
t = mul(t, t);
}
return ret;
}
int cal_root(int mod)
{
int factor[], num = , s = mod - ;
MOD = mod--;
for (int i = ; i * i <= s; i++){
if (s % i == ){
factor[num++] = i;
while (s % i == )s /= i;
}
}
if (s != )factor[num++] = s;
for (int i = ;; i++){
int j = ;
for (; j < num && power(i, mod / factor[j]) != ; j++);
if (j == num)return i;
}
}
struct Complex {
long double r , i ;
Complex () {}
Complex ( double r , double i ) : r ( r ) , i ( i ) {}
Complex operator + ( const Complex& t ) const {
return Complex ( r + t.r , i + t.i ) ;
}
Complex operator - ( const Complex& t ) const {
return Complex ( r - t.r , i - t.i ) ;
}
Complex operator * ( const Complex& t ) const {
return Complex ( r * t.r - i * t.i , r * t.i + i * t.r ) ;
}
} ; void FFT ( Complex y[] , int n , int rev ) {
for ( int i = , j , t , k ; i < n ; ++ i ) {
for ( j = , t = i , k = n >> ; k ; k >>= , t >>= ) j = j << | t & ;
if ( i < j ) swap ( y[i] , y[j] ) ;
}
for ( int s = , ds = ; s <= n ; ds = s , s <<= ) {
Complex wn = Complex ( cos ( rev * * pi / s ) , sin ( rev * * pi / s ) ) , w ( , ) , t ;
for ( int k = ; k < ds ; ++ k , w = w * wn ) {
for ( int i = k ; i < n ; i += s ) {
y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;
y[i] = y[i] + t ;
}
}
}
if ( rev == - ) for ( int i = ; i < n ; ++ i ) y[i].r /= n ;
} Complex s[N];
int T,n,m,x,num[N];
LL ans[N],mo[N],fmo[N];
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
int G = cal_root(m);
for(LL i = , t = ; i < m-; ++i,t = t*G%m)
mo[i] = t,fmo[t] = i;
memset(num,,sizeof(num));
LL cnt0 = ;
for(int i = ; i <= m; ++i) ans[i] = ;
for(int i = ; i <= n; ++i) {
scanf("%d",&x);
x%=m;
if(x == ) cnt0++;
else {
num[fmo[x]]++;
}
}
int n1 = ;
for(n1=;n1<=(*m-);n1<<=);
for(int i = ; i < m; ++i) s[i] = Complex(num[i],);
for(int i = m; i < n1; ++i) s[i] = Complex(,);
FFT(s,n1,);
for(int i = ; i < n1; ++i) s[i] = s[i]*s[i];
FFT(s,n1,-);
printf("%lld\n",(LL)cnt0*(n-cnt0)+(LL)cnt0*(cnt0-)/);
for(int i = ; i <= *m-; ++i) {
LL now = (LL)(s[i].r+0.5);
if(i%==) now -= num[i/];
now/=;
ans[mo[i%(m-)]] += now;
}
for(int i = ; i < m; ++i) printf("%lld\n",ans[i]);
}
return ;
}
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = +, M = 1e3+,inf = 2e9; int MOD;
inline int mul2(int a, int b){
return (long long)a * b % MOD;
}
int power(int a, int b){
int ret = ;
for (int t = a; b; b >>= ){
if (b & )ret = mul2(ret, t);
t = mul2(t, t);
}
return ret;
}
int cal_root(int mod)
{
int factor[], num = , s = mod - ;
MOD = mod--;
for (int i = ; i * i <= s; i++){
if (s % i == ){
factor[num++] = i;
while (s % i == )s /= i;
}
}
if (s != )factor[num++] = s;
for (int i = ;; i++){
int j = ;
for (; j < num && power(i, mod / factor[j]) != ; j++);
if (j == num)return i;
}
} LL P,G;
LL mul(LL x,LL y){
return (x*y-(LL)(x/(long double)P*y+1e-)*P+P)%P;
}
LL qpow(LL x,LL k,LL p){
LL ret=;
while(k){
if(k&) ret=mul(ret,x);
k>>=;
x=mul(x,x);
}
return ret;
}
LL wn[];
void getwn(){
for(int i=; i<=; ++i){
int t=<<i;
wn[i]=qpow(G,(P-)/t,P);
}
}
void NTT_init() {
P = 3221225473LL,G = ;
getwn();
}
int len;
void NTT(LL y[],int op){
for(int i=,j=len>>,k; i<len-; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>;
while(j>=k){
j-=k;
k>>=;
}
if(j<k) j+=k;
}
int id=;
for(int h=; h<=len; h<<=) {
++id;
for(int i=; i<len; i+=h){
LL w=;
for(int j=i; j<i+(h>>); ++j){
LL u=y[j],t=mul(y[j+h/],w);
y[j]=u+t;
if(y[j]>=P) y[j]-=P;
y[j+h/]=u-t+P;
if(y[j+h/]>=P) y[j+h/]-=P;
w=mul(w,wn[id]);
}
}
}
if(op==-){
for(int i=; i<len/; ++i) swap(y[i],y[len-i]);
LL inv=qpow(len,P-,P);
for(int i=; i<len; ++i) y[i]=mul(y[i],inv);
}
}
LL s[N];
int T,n,m;
LL ans[N];
int num[N],mo[N],fmo[N],root;
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
root = cal_root(m);
LL cnt0 = ;
memset(ans,,sizeof(ans));
memset(num,,sizeof(num));
for(LL i = , t = ; i < m-; ++i,t=t*root%m)
mo[i] = t,fmo[t] = i;
for(int i = ; i <= n; ++i) {
int x;
scanf("%d",&x);
x%=m;
if(x == ) cnt0++;
else num[fmo[x]]++;
}
for(len = ; len <= (*m-); len<<=);
for(int i = ;i < m; ++i) s[i] = num[i];
for(int i = m; i < len; ++i) s[i] = ;
NTT_init();
NTT(s,);
for(int i = ; i < len; ++i) s[i] = mul(s[i],s[i]);
NTT(s,-);
for(int i = ; i <= *m-; ++i) {
LL now = s[i];
if(i%==) now -= num[i/];
now /= ;
ans[mo[i%(m-)]] += now;
}
printf("%lld\n",(LL)cnt0*(n-cnt0)+(LL)cnt0*(cnt0-)/);
for(int i = ; i < m; ++i) printf("%lld\n",ans[i]);
}
return ;
}
XJTUOJ wmq的A×B Problem FFT/NTT的更多相关文章
- FFT/NTT模板 既 HDU1402 A * B Problem Plus
@(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...
- FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ
众所周知,tzc 在 2019 年(12 月 31 日)就第一次开始接触多项式相关算法,可到 2021 年(1 月 1 日)才开始写这篇 blog. 感觉自己开了个大坑( 多项式 多项式乘法 好吧这个 ...
- [学习笔记&教程] 信号, 集合, 多项式, 以及各种卷积性变换 (FFT,NTT,FWT,FMT)
目录 信号, 集合, 多项式, 以及卷积性变换 卷积 卷积性变换 傅里叶变换与信号 引入: 信号分析 变换的基础: 复数 傅里叶变换 离散傅里叶变换 FFT 与多项式 \(n\) 次单位复根 消去引理 ...
- FFT/NTT/MTT学习笔记
FFT/NTT/MTT Tags:数学 作业部落 评论地址 前言 这是网上的优秀博客 并不建议初学者看我的博客,因为我也不是很了解FFT的具体原理 一.概述 两个多项式相乘,不用\(N^2\),通过\ ...
- FFT&NTT总结
FFT&NTT总结 一些概念 \(DFT:\)离散傅里叶变换\(\rightarrow O(n^2)\)计算多项式卷积 \(FFT:\)快速傅里叶变换\(\rightarrow O(nlogn ...
- FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅲ
第三波,走起~~ FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅱ 单位根反演 今天打多校时 1002 被卡科技了 ...
- FFT \ NTT总结(多项式的构造方法)
前言.FFT NTT 算法 网上有很多,这里不再赘述. 模板见我的代码库: FFT:戳我 NTT:戳我 正经向:FFT题目解题思路 \(FFT\)这个玩意不可能直接裸考的..... 其实一般\(FF ...
- 我的第三篇博客(激动激动真激动!!!)A-B Problem
#210. 差(A-B problem) 题目描述 楠楠在网上刷题,感觉第一题:求两数的和(A+B Problem)太无聊了,于是增加了一题:A-B Problem,难倒了一群小朋友,哈哈. 题目是这 ...
- A-B Problem nyoj
A-B Problem 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧. 现在有两个实数A和 ...
随机推荐
- 雅礼培训 Problem B 【图论 + 贪心】
题意 A和B在树上轮流选点,记A的联通块个数为\(x\),B的联通块个数为\(y\) A使\(x - y\)最大,B使\(x - y\) 二人采取最优策略,求\(x-y\) 题解 树联通块个数 = 点 ...
- 算法复习——序列分治(ssoj光荣的梦想)
题目: 题目描述 Prince对他在这片大陆上维护的秩序感到满意,于是决定启程离开艾泽拉斯.在他动身之前,Prince决定赋予King_Bette最强大的能量以守护世界.保卫这里的平衡与和谐.在那个时 ...
- 【NOI Linux】复习一波命令行
$linux$ 终端真是用不惯. 假设 a 是一个可执行文件( $linux$ 下的可执行文件没有后缀 ) 1. size a 计算一个程序的静态内存(全局数组变量.栈空间.堆空间等),单位是字节.除 ...
- R语言入门视频笔记--3-1--矩阵与数组
生成一个新矩阵,多用一些参数吧这次: x <- c(12,13,14,15) rname <- c("R1","R2") nname <- c ...
- Linux 环境下思源黑体字体与 Java 之间的兼容性问题的解决(补充说明)
在前一篇随笔中,我讲了一下有关 Linux 环境下思源黑体与 Java 之间的兼容性问题,后来经过测试发现,默认安装的思源黑体字体同时包含简体字体和繁体字体,并且其对应的语言编码也是不同的.尝试着把繁 ...
- jquery.fullPage.js全屏滚动插件
注:本文内容复制于http://www.51xuediannao.com/js/jquery/jquery.fullPage.html 和 http://www.360doc.com/content/ ...
- html页面中拍照和上传照片那些事儿(二)
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6895352.html 本文主要说下iOS上传的照片在安卓机 ...
- 【postMan】发送post请求,返回错误码415
解决方法: 参看:https://www.cnblogs.com/spec-dog/p/3731279.html 将Request的Content-Type:application/json;char ...
- 【Windows7】win7启动 报错 AutoIt错误,不能打开脚本文件
解决方法: 1.开始-->运行-->regedit 2.按照下图 3.
- js上传文件研究
https://github.com/shengulong/javascript-file-upload