FFT 变化

 

hdu 4609 3-idiots FFT

分类: 模版 数学 stl   81人阅读  评论(0)  收藏  举报
[cpp]  view plain copy
  1. /* 
  2. hdu 4609 3-idiots FFT 
  3. http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 
  4. */  
  5. #pragma warning(disable : 4786)  
  6. #pragma comment(linker, "/STACK:102400000,102400000")  
  7. #include <iostream>  
  8. #include <string>  
  9. #include <cstring>  
  10. #include <cstdlib>  
  11. #include <cstdio>  
  12. #include <cmath>  
  13. #include <algorithm>  
  14. #include <functional>  
  15. #include <map>  
  16. #include <set>  
  17. #include <vector>  
  18. #include <stack>  
  19. #include <queue>//priority_queue  
  20. #include <bitset>  
  21. #include <complex>  
  22. #include <utility>  
  23. /* 
  24. **make_pair() 
  25. **/  
  26. using namespace std;  
  27. const double eps=1e-8;  
  28. const double PI=acos(-1.0);  
  29. const int inf=0x7fffffff;  
  30. template<typename T> inline T MIN(T a,T b){return a<b?a:b;}  
  31. template<typename T> inline T MAX(T a,T b){return a>b?a:b;}  
  32. template<typename T> inline T SQR(T a){return a*a;}  
  33. inline int dbcmp(double a){return a>eps?(1):(a<(-eps)?(-1):0);}  
  34.   
  35. typedef __int64 LL;  
  36. int n;  
  37. const int size=400040;  
  38. int data[size/4];  
  39. LL num[size],sum[size];  
  40. complex<double> x1[size];  
  41.   
  42.   
  43.   
  44. void change(complex<double> y[],int len)  
  45. {  
  46.     int i,j,k;  
  47.     for(i = 1, j = len/2;i < len-1;i++)  
  48.     {  
  49.         if(i < j)swap(y[i],y[j]);  
  50.         k = len/2;  
  51.         while( j >= k)  
  52.         {  
  53.             j -= k;  
  54.             k /= 2;  
  55.         }  
  56.         if(j < k)j += k;  
  57.     }  
  58. }  
  59. void fft(complex<double> y[],int len,int on)  
  60. {  
  61.     change(y,len);  
  62.     for(int h = 2;h <= len;h <<= 1)  
  63.     {  
  64.         complex<double> wn(cos(-on*2*PI/h),sin(-on*2*PI/h));  
  65.         for(int j = 0;j < len;j += h)  
  66.         {  
  67.             complex<double> w(1,0);  
  68.             for(int k = j;k < j+h/2;k++)  
  69.             {  
  70.                 complex<double> u = y[k];  
  71.                 complex<double> t = w*y[k+h/2];  
  72.                 y[k] = u+t;  
  73.                 y[k+h/2] = u-t;  
  74.                 w = w*wn;  
  75.             }  
  76.         }  
  77.     }  
  78.     if(on == -1)  
  79.         for(int i = 0;i < len;i++)  
  80.             y[i].real(y[i].real()/len);  
  81. }  
  82. int main() {  
  83.     // your code goes here  
  84.     int t,i;  
  85.     cin>>t;  
  86.     while(t--)  
  87.     {  
  88.         cin>>n;  
  89.         memset(num,0,sizeof(num));  
  90.         for(i=0;i<n;++i)  
  91.         {  
  92.             cin>>data[i];  
  93.             num[data[i]]++;  
  94.         }  
  95.         sort(data,data+n);  
  96.         int len1=data[n-1]+1;  
  97.         int len=1;  
  98.         while(len<2*len1) len<<=1;  
  99.         for(i=0;i<len1;++i)  
  100.         {  
  101.             x1[i]=complex<double>(num[i],0);  
  102.         }  
  103.         for(;i<len;++i)  
  104.         {  
  105.             x1[i]=complex<double>(0,0);  
  106.         }  
  107.         fft(x1,len,1);  
  108.         for(i=0;i<len;++i)  
  109.         {  
  110.             x1[i]=x1[i]*x1[i];  
  111.         }  
  112.         fft(x1,len,-1);  
  113.   
  114.         for(i=0;i<len;++i)  
  115.         {  
  116.             num[i]=(LL)(x1[i].real()+0.5);  
  117.         }  
  118.   
  119.         len=2*data[n-1];  
  120.         for(i=0;i<n;++i)  
  121.         {  
  122.             num[data[i]+data[i]]--;  
  123.         }  
  124.         for(i=1;i<=len;++i)  
  125.         {  
  126.             num[i]/=2;  
  127.         }  
  128.         sum[0]=0;  
  129.         for(i=1;i<=len;++i)  
  130.         {  
  131.             sum[i]=sum[i-1]+num[i];  
  132.         }  
  133.         LL cnt=0;  
  134.         for(i=0;i<n;++i)  
  135.         {  
  136.             cnt+=sum[len]-sum[data[i]];  
  137.             //减掉一个取大一个取小的情况,违背了data[i]是最长边的规则  
  138.             cnt-=(LL)(n-1-i)*i;  
  139.             //减掉一个取本身,一个取其他数的情况,否则data[i]就被取了两次  
  140.             cnt-=(LL)(n-1);  
  141.             //减掉取两个比他大的数的情况,违背了data[i]是最长边的规则  
  142.             cnt-=(LL)(n-1-i)*(n-2-i)/2;  
  143.         }  
  144.         LL tot = (LL)n*(n-1)*(n-2)/6;  
  145.         printf("%.7lf\n",(double)cnt/tot);  
  146.     }  
  147.     return 0;  
  148. }  



 

hdu 1402 A * B Problem Plus FFT

分类: 模版 stl 数学   82人阅读  评论(0)  收藏  举报
[cpp]  view plain copy
  1. /* 
  2. hdu 1402 A * B Problem Plus FFT 
  3.  
  4. 这是我的第二道FFT的题 
  5.  
  6. 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 
  7.  
  8. 这个代码是在前一题的基础上改的 
  9.  
  10. 做完这个题,我才有点儿感觉,原来FFT在这里就是加速大整数乘法而已 
  11.  
  12. 像前一题,也是一个大整数乘法,然后去掉一些非法的情况 
  13.  
  14. */  
  15. #pragma warning(disable : 4786)  
  16. #pragma comment(linker, "/STACK:102400000,102400000")  
  17. #include <iostream>  
  18. #include <string>  
  19. #include <cstring>  
  20. #include <cstdlib>  
  21. #include <cstdio>  
  22. #include <cmath>  
  23. #include <algorithm>  
  24. #include <functional>  
  25. #include <map>  
  26. #include <set>  
  27. #include <vector>  
  28. #include <stack>  
  29. #include <queue>//priority_queue  
  30. #include <bitset>  
  31. #include <complex>  
  32. #include <utility>  
  33. /* 
  34. **make_pair() 
  35. **/  
  36. using namespace std;  
  37. const double eps=1e-8;  
  38. const double PI=acos(-1.0);  
  39. const int inf=0x7fffffff;  
  40. template<typename T> inline T MIN(T a,T b){return a<b?a:b;}  
  41. template<typename T> inline T MAX(T a,T b){return a>b?a:b;}  
  42. template<typename T> inline T SQR(T a){return a*a;}  
  43. inline int dbcmp(double a){return a>eps?(1):(a<(-eps)?(-1):0);}  
  44.   
  45. typedef __int64 LL;  
  46. int n;  
  47. const int size=400040;  
  48. int data[size/4];  
  49. int sum[size];  
  50. complex<double> x1[size],num1[size],num2[size];  
  51.   
  52.   
  53.   
  54. void change(complex<double> y[],int len)  
  55. {  
  56.     int i,j,k;  
  57.     for(i = 1, j = len/2;i < len-1;i++)  
  58.     {  
  59.         if(i < j)swap(y[i],y[j]);  
  60.         k = len/2;  
  61.         while( j >= k)  
  62.         {  
  63.             j -= k;  
  64.             k /= 2;  
  65.         }  
  66.         if(j < k)j += k;  
  67.     }  
  68. }  
  69. void fft(complex<double> y[],int len,int on)  
  70. {  
  71.     change(y,len);  
  72.     for(int h = 2;h <= len;h <<= 1)  
  73.     {  
  74.         complex<double> wn(cos(-on*2*PI/h),sin(-on*2*PI/h));  
  75.         for(int j = 0;j < len;j += h)  
  76.         {  
  77.             complex<double> w(1,0);  
  78.             for(int k = j;k < j+h/2;k++)  
  79.             {  
  80.                 complex<double> u = y[k];  
  81.                 complex<double> t = w*y[k+h/2];  
  82.                 y[k] = u+t;  
  83.                 y[k+h/2] = u-t;  
  84.                 w = w*wn;  
  85.             }  
  86.         }  
  87.     }  
  88.     if(on == -1)  
  89.         for(int i = 0;i < len;i++)  
  90.             y[i].real(y[i].real()/len);  
  91. }  
  92. char s1[size];  
  93. char s2[size];  
  94. int main() {  
  95.     // your code goes here  
  96.     int t,i,j;  
  97.     int len,len1,len2;  
  98.     //cin>>t;  
  99.     while(gets(s1))  
  100.     {  
  101.         gets(s2);  
  102.         memset(num1,0,sizeof(num1));  
  103.         memset(num2,0,sizeof(num2));  
  104.         len1=strlen(s1);  
  105.         len2=strlen(s2);  
  106.         //printf("%d %d",len1,len2);  
  107.         len=1;  
  108.         while(len<2*len1||len<len2*2) len<<=1;  
  109.         for(i=len1-1,j=0;i>=0;--i,++j)  
  110.         {  
  111.             num1[j]=complex<double>(s1[i]-'0',0);  
  112.         }  
  113.         while(j<=len)  
  114.         {  
  115.             num1[j]=complex<double>(0,0);  
  116.             ++j;  
  117.         }  
  118.         for(i=len2-1,j=0;i>=0;--i,++j)  
  119.         {  
  120.             num2[j]=complex<double>(s2[i]-'0',0);  
  121.         }  
  122.         while(j<=len)  
  123.         {  
  124.             num2[j]=complex<double>(0,0);  
  125.             ++j;  
  126.         }  
  127.         fft(num1,len,1);  
  128.         fft(num2,len,1);  
  129.         for(i=0;i<len;++i)  
  130.         {  
  131.             num1[i]=num1[i]*num2[i];  
  132.         }  
  133.         fft(num1,len,-1);  
  134.   
  135.         for(i=0;i<len;++i)  
  136.         {  
  137.             sum[i]=(int)(num1[i].real()+0.5);  
  138.         }  
  139.         for(i=0;i<len;++i)  
  140.         {  
  141.             sum[i+1]+=sum[i]/10;  
  142.             sum[i]=sum[i]%10;  
  143.         }  
  144.         len=len1+len2-1;  
  145.         while(sum[len]<=0&&len>0) --len;  
  146.         for(;len>=0;--len)  
  147.         {  
  148.             printf("%c",sum[len]+'0');  
  149.         }  
  150.         printf("\n");  
  151.     }  
  152.     return 0;  
  153. }  

你可能感兴趣的:(数学,数学,STL,STL,模版,模版)