高斯消元法HDU - 5833 Zhu and 772002

举报
用户已注销 发表于 2021/11/19 03:06:57 2021/11/19
【摘要】 目录 一,高斯消元法解异或方程 二,OJ实战 HDU - 5833 Zhu and 772002 UVA - 11542 Square 一,高斯消元法解异或方程 从左往右依次找到每一列的一个不为0的元素(如果有的话),用消元法消去这一列其他行的不为0的数,直到整个方程求解出来。   二,OJ实战 HDU...

目录

一,高斯消元法解异或方程

二,OJ实战

HDU - 5833 Zhu and 772002

UVA - 11542 Square


一,高斯消元法解异或方程

从左往右依次找到每一列的一个不为0的元素(如果有的话),用消元法消去这一列其他行的不为0的数,直到整个方程求解出来。

 

二,OJ实战

HDU - 5833 Zhu and 772002

题目:

Description

Zhu and 772002 are both good at math. One day, Zhu wants to test the ability of 772002, so he asks 772002 to solve a math problem. 

But 772002 has a appointment with his girl friend. So 772002 gives this problem to you. 

There are   numbers  . The value of the prime factors of each number does not exceed  , you can choose at least one number and multiply them, then you can get a number  . 

How many different ways of choices can make   is a perfect square number. The answer maybe too large, so you should output the answer modulo by   1000000007
Input

First line is a positive integer T, represents there are T test cases. 

For each test case: 

First line includes a number  ,next line there are   numbers  .
Output

For the i-th test case , first output Case #i: in a single line. 

Then output the answer of i-th test case modulo by 1000000007
Sample Input

2
3
3 3 4
3
2 2 2
Sample Output

Case #1:
3
Case #2:
3

我的思路是,把2000以内的303个素数用数组存起来,然后把输入的n个数表示成n行,每一行都是303个0或者1。

1表示的是这个数中这个素因子的次数为奇数,0表示为偶数。

现在就是要求,有多少种选行的方法,使得行的和全为0。

这里的和,可以理解为普通的和mod2,也可以理解为异或。

然后怎么求呢,这就是高斯消元法了。

本来应该能对的,可惜我傻了,偷懒了,没有真的把每个数表示成1个数组,用了差不多的形式。

然后就悲催了,那么多素数,它们的积一下就超了long long了,然后就wrong answer了。

错误代码:
 


      #include <iostream>
      #include <stdio.h>
      #include<vector>
      using namespace std;
      long long v[305];
      int prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999 };
      int p;
      long long huajian(long long a)
      {
         for (int i = 0; i < 303; i++)
          {
             int pp = prime[i];
             while (a % (pp*pp) == 0)a /= (pp*pp);
          }
         return a;
      }
      int main()
      {
         int t, n, r, num;
         long long a;
          cin >> t;
         for (int i = 1; i <= t; i++)
          {
              cin >> n;
              r = 1;
              num = 0;
             while (n--)
              {
                  cin >> a;
                  a = huajian(a);
                 if (a == 1)r = r * 2 % 1000000007;
                 else v[num++] = a;
              }
             for (int i = 0; i < 303; i++)
              {
                 if (num == 0)break;
                  p = prime[i];
                 int key;
                 for (key = 0; key < num; key++)if (v[key] % p == 0)break;
                 if (key<num)
                  {
                     for (int j = key + 1; j < num;j++)
                      {
                         if (v[j] % p == 0)v[j] = huajian(v[j] * v[key]);/long long
                         if (v[j] == 1)
                          {
                              r = r * 2 % 1000000007;
                              v[j--] = v[num-- - 1];
                          }
                      }
                      v[key] = v[num-- - 1];
                  }
              }
             printf("Case #%d:\n%d\n", i, (r-1) % 1000000007);
          }
         return 0;
      }
  
 

然后现在又按照二维数组来写,写完就AC了。

代码:


      #include <iostream>
      using namespace std;
      int prime[303] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999 };
      int x[300][303];
      bool flag[300];
      int t, n, p;
      long long a;
      int main()
      {
      	cin >> t;
     	for (int i = 1; i <= t; i++)
      	{
      		cin >> n;
     		int row = 0;
     		while (n--)
      		{
      			cin >> a;
     			for (int i = 0; i < 303; i++)
      			{
      				p = prime[i];
     				while (a % (p*p) == 0)a /= (p*p);
      				x[row][i] = (a%p == 0);
      			}
      			flag[row] = true;
      			row++;
      		}
     	for (int i = 0; i < 303; i++)for (int j = 0; j < row; j++)if (flag[j] && x[j][i])
      	{
     		for (int k = j + 1; k < row; k++)if (flag[k] && x[k][i])
     		for (int xi = 0; xi < 303; xi++)x[k][xi] ^= x[j][xi];
      		flag[j] = false;
     		break;
      	}
     		int r = 1, ji;
     		for (int i = 0; i < row; i++)
      		{
      			ji = 0;
     			if (flag[i])ji = 1;
     			for (int j = 0; j < row; j++)if (x[i][j])ji = 0;
     			if (ji)r = r * 2 % 1000000007;
      		}
     		printf("Case #%d:\n%d\n", i, (r - 1) % 1000000007);
      	}
     	return 0;
      }
  
 

UVA - 11542 Square

 

题目:

这个题目完全就是HDU - 5833 Zhu and 772002 的简化版

所以我直接把那个题目的代码改了一丢丢就可以AC了。

代码:


      #include <iostream>
      using namespace std;
      int prime[303] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999 };
      int x[300][303];
      bool flag[300];
      int t, n, p;
      long long a;
      int main()
      {
      	cin >> t;
     	for (int i = 1; i <= t; i++)
      	{
      		cin >> n;
     		int row = 0;
     		while (n--)
      		{
      			cin >> a;
     			for (int i = 0; i < 303; i++)
      			{
      				p = prime[i];
     				while (a % (p*p) == 0)a /= (p*p);
      				x[row][i] = (a%p == 0);
      			}
      			flag[row] = true;
      			row++;
      		}
     	for (int i = 0; i < 303; i++)for (int j = 0; j < row; j++)if (flag[j] && x[j][i])
      	{
     		for (int k = j + 1; k < row; k++)if (flag[k] && x[k][i])
     		for (int xi = 0; xi < 303; xi++)x[k][xi] ^= x[j][xi];
      		flag[j] = false;
     		break;
      	}
     		long long r = 1, ji;
     		for (int i = 0; i < row; i++)
      		{
      			ji = 0;
     			if (flag[i])ji = 1;
     			for (int j = 0; j < row; j++)if (x[i][j])ji = 0;
     			if (ji)r = r * 2 ;
      		}
      		cout<<r-1<<endl;
      	}
     	return 0;
      }
  
 

 

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/115530383

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。