N-dimensional Sphere

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 668    Accepted Submission(s): 234

Problem Description
In an N-dimensional space, a sphere is defined as {(x1, x2 ... xN)| ∑(xi-Xi)^2 = R^2 (i=1,2,...,N) }. where (X1,X2…XN) is the center. You're given N + 1 points on an N-dimensional sphere and are asked to calculate the center of the sphere.
 
Input
The first line contains an integer T which is the number of test cases.
For each case there's one integer N on the first line.
Each of the N+1 following lines contains N integers x1, x2 ... xN describing the coordinate of a point on the N-dimensional sphere.
(0 <= T <= 10, 1 <= N <= 50, |xi| <= 10^17)
 
Output
For the kth case, first output a line contains “Case k:”, then output N integers on a line indicating the center of the N-dimensional sphere
(It's guaranteed that all coordinate components of the answer are integers and there is only one solution and |Xi| <= 10^17)
 
Sample Input
2
2
1 0
-1 0
0 1
3
2 2 3
0 2 3
1 3 3
1 2 4
 
Sample Output
Case 1:
0 0
Case 2:
1 2 3
 
 
 
这条题目的做法很容易想出来 。
凭借 n + 1 个点代入 n 维圆公式, 求圆心 。
然后用第 n + 1 个方程( 设下标为n )  sigma( ( Xi - Oi )^2 )  = R^2 
跟前n 个方程联立容易得到 :
  sigma( ( Xi - Oi )^2 )  =  sigma( ( Yi - Oi )^2 )  
两边都展开然后消掉Oi^2就得到
  sigma(  2*( Xi - Yi )*Oi ) = sigma(  Xi^2 - Yi^2 )  .
得到 n 个这样的 n 元一次方程之后就可以利用高斯消元解决。
 
但首先 fabs( xi ) <= 1e17 的。 大数据的话显然计算过程溢出 。
就用到  sigma( ai * xi ) = an ( % mod ) 来解决。 求得解依然唯一。
 
在高斯消元的过程中会有除法 , 用求逆来解决。
由于数据很大, 欧拉定理会溢 , 那么用扩展欧几里得就OK 。
 
然后还需要将数据加一个偏移差,把所有数据处理成正数 (相当于把整个图形平移了,最后减回来不影响结果)。
避免在取余过程中把(负数+mod)%mod弄成了正。
 
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <cmath>
  6. #include <vector>
  7. #include <queue>
  8. #include <map>
  9. #include <set>
  10. #include <stack>
  11. #include <algorithm>
  12. using namespace std;
  13. #define root 1,n,1
  14. #define lson l,mid,rt<<1
  15. #define rson mid+1,r,rt<<1|1
  16. #define lr rt<<1
  17. #define rr rt<<1|1
  18. typedef long long LL;
  19. typedef pair<int,int>pii;
  20. #define X first
  21. #define Y second
  22. const int oo = 1e9+;
  23. const double PI = acos(-1.0);
  24. const double eps = 1e- ;
  25. const int N = ;
  26. #define mod 200000000000000003LL
  27. #define dif 100000000000000000LL
  28.  
  29. LL Mod(LL x) {
  30. if (x >= mod) return x - mod;
  31. return x;
  32. }
  33. LL mul(LL a, LL b) {
  34. LL res;
  35. for (res = ; b; b >>= ) {
  36. if (b & )
  37. res = Mod(res + a);
  38. a = Mod(a + a);
  39. }
  40. return res;
  41. }
  42.  
  43. void e_gcd( LL a , LL b , LL &d , LL &x , LL &y ) {
  44. if( !b ){ d = a , x = , y = ; return ; }
  45. e_gcd( b , a%b , d , y , x );
  46. y -= x*(a/b);
  47. }
  48.  
  49. LL inv( LL a , LL n ){
  50. LL d,x,y ;
  51. e_gcd(a,n,d,x,y);
  52. return ( x % n + n ) % n ;
  53. }
  54.  
  55. LL A[N][N] , g[N][N];
  56. int n ;
  57.  
  58. void Gauss() {
  59.  
  60. for( int i = ; i < n ; ++i ) {
  61. int r = i ;
  62. for( int j = i ; j < n ; ++j ) {
  63. if( g[j][i] ) { r = j ; break ; }
  64. }
  65. if( r != i ) for( int j = ; j <= n ; ++j ) swap( g[i][j] , g[r][j] ) ;
  66.  
  67. LL INV = inv( g[i][i] , mod );
  68. for( int k = i + ; k < n ; ++k ) {
  69. if( g[k][i] ) {
  70. LL f = mul( g[k][i] , INV );
  71. for( int j = i ; j <= n ; ++j ) {
  72. g[k][j] -= mul( f , g[i][j] );
  73. g[k][j] = ( g[k][j] % mod + mod ) % mod ;
  74. }
  75. }
  76. }
  77. }
  78. for( int i = n - ; i >= ; --i ){
  79. for( int j = i + ; j < n ; ++j ){
  80. g[i][n] -= mul( g[j][n] , g[i][j] ) , g[i][n] += mod , g[i][n] %= mod ;
  81. }
  82. g[i][n] = mul( g[i][n] , inv( g[i][i] , mod ) );
  83. }
  84. }
  85.  
  86. void Run() {
  87.  
  88. scanf("%d",&n);
  89. memset( g , , sizeof g );
  90. for( int i = ; i <= n ; ++i ) {
  91. for( int j = ; j < n ; ++j ) {
  92. scanf("%I64d",&A[i][j]);
  93. A[i][j] += dif ;
  94. }
  95. }
  96.  
  97. for( int i = ; i < n ; ++i ){
  98. for( int j = ; j < n ; ++j ){
  99. g[i][j] = Mod( A[n][j] - A[i][j] + mod );
  100. g[i][j] = mul( g[i][j] , ) ;
  101. g[i][n] = Mod( g[i][n] + mul( A[n][j] , A[n][j] ) );
  102. g[i][n] = Mod( g[i][n] - mul( A[i][j] , A[i][j] ) + mod );
  103. }
  104. }
  105.  
  106. Gauss();
  107. printf("%I64d",g[][n]-dif);
  108. for( int i = ; i < n ; ++i ){
  109. printf(" %I64d",g[i][n]-dif);
  110. }puts("");
  111. }
  112.  
  113. int main()
  114. {
  115. #ifdef LOCAL
  116. freopen("in.txt","r",stdin);
  117. #endif // LOCAL
  118. int cas = , _ ; scanf("%d",&_ );
  119. while( _-- ){
  120. printf("Case %d:\n",cas++); Run();
  121. }
  122. }

HDU 3571 N-dimensional Sphere( 高斯消元+ 同余 )的更多相关文章

  1. HDU.3571.N-dimensional Sphere(高斯消元 模线性方程组)

    题目链接 高斯消元详解 /* $Description$ 在n维空间中给定n+1个点,求一个点使得这个点到所有点的距离都为R(R不给出).点的任一坐标|xi|<=1e17. $Solution$ ...

  2. BZOJ-1013 球形空间产生器sphere 高斯消元+数论推公式

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3662 Solved: 1910 [Subm ...

  3. BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/Judg ...

  4. HDU 5755 Gambler Bo(高斯消元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...

  5. HDU 4818 RP problem (高斯消元, 2013年长春区域赛F题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4818 深深地补一个坑~~~ 现场赛坑在这题了,TAT.... 今天把代码改了下,过掉了,TAT 很明显 ...

  6. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  7. lydsy1013: [JSOI2008]球形空间产生器sphere 高斯消元

    题链:http://www.lydsy.com/JudgeOnline/problem.php?id=1013 1013: [JSOI2008]球形空间产生器sphere 时间限制: 1 Sec  内 ...

  8. [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...

  9. hdu 5833 Zhu and 772002 高斯消元

    Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...

随机推荐

  1. 【JAVA】java编译错误:编码UTF8/GBK的不可映射字符

    环境: win7 cmd窗口编译 javac xx.java时报错 错误显示:错误:编码GBK的不可映射字符 背景: 分析发现是中文字符所在行报错了 查阅相关资料发现,是因为编译器设置为了utf-8, ...

  2. MySQL对字段新增自增序列

    现在有这样的场景,我们的数据库类型是MySQL,表是从其他库拿过来的,约束和索引都没迁移.现在希望增加一个自增序列. 且自增序列是从当前最大自增ID开始的,下面就是这样一个过程的演示. mysql&g ...

  3. DTS

    一.DTS的加载过程   如果要使用Device Tree,首先用户要了解自己的硬件配置和系统运行参数,并把这些信息组织成Device Tree source file.通过DTC(Device Tr ...

  4. geometry_msgs的ros message 类型赋值

    test_custom_particles.cpp // // Created by gary on 2019/8/27. // #include <ros/ros.h> #include ...

  5. 数据结构 java概况

    数据结构可以分为三种结构: 线性结构: 数组:栈:队列:链表:哈希表 树结构: 二叉树,二分搜索树,AVL,红黑树,Treap,Splay,堆,Trie,线段树,K-D树,并查集,哈夫曼树 图结构 邻 ...

  6. python常用函数 H

    heapify(iterable) 堆排序. 例子: heappop(iterable) 弹出堆排序的第一个元素,即最小值. 例子: hasattr(object,attr) 用于确定对象是否有某个属 ...

  7. bootz to be continued

    dmesgcat /proc/interrupts cat /proc/meminfocat /proc/cpuinfo top bootz 0x10000000 0x12000000 0x11000 ...

  8. BZOJ5261 Rhyme

    传送门 广义后缀自动机= =+ 跟ptx大爷的博客学的 戳我传送 我写的第一种 建立Trie树的写法 bfs建立SAM 为什么是bfs呢 我也不知道(GG) 经过我一番抱大腿+询问 各位大爷说的原因是 ...

  9. NCRE训练二

    package com.fei.ncre; import java.io.RandomAccessFile; /** * 该程序的功能是将本程序代码打印输出 */ public class Java_ ...

  10. 设置Oracle PL/SQL时间显示格式NLS_TIMESTAMP_FORMAT

    Oracle中TIMESTAMP时间的显示格式   Oracle数据库的时间字段我们通常是使用timestamp 格式,在未做设置前, 查询出来的数据类似于“27-1月 -08 12.04.35.87 ...