高斯消元法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了。

错误代码:
 


  
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include<vector>
  4. using namespace std;
  5. long long v[305];
  6. 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 };
  7. int p;
  8. long long huajian(long long a)
  9. {
  10. for (int i = 0; i < 303; i++)
  11. {
  12. int pp = prime[i];
  13. while (a % (pp*pp) == 0)a /= (pp*pp);
  14. }
  15. return a;
  16. }
  17. int main()
  18. {
  19. int t, n, r, num;
  20. long long a;
  21. cin >> t;
  22. for (int i = 1; i <= t; i++)
  23. {
  24. cin >> n;
  25. r = 1;
  26. num = 0;
  27. while (n--)
  28. {
  29. cin >> a;
  30. a = huajian(a);
  31. if (a == 1)r = r * 2 % 1000000007;
  32. else v[num++] = a;
  33. }
  34. for (int i = 0; i < 303; i++)
  35. {
  36. if (num == 0)break;
  37. p = prime[i];
  38. int key;
  39. for (key = 0; key < num; key++)if (v[key] % p == 0)break;
  40. if (key<num)
  41. {
  42. for (int j = key + 1; j < num;j++)
  43. {
  44. if (v[j] % p == 0)v[j] = huajian(v[j] * v[key]);/超long long
  45. if (v[j] == 1)
  46. {
  47. r = r * 2 % 1000000007;
  48. v[j--] = v[num-- - 1];
  49. }
  50. }
  51. v[key] = v[num-- - 1];
  52. }
  53. }
  54. printf("Case #%d:\n%d\n", i, (r-1) % 1000000007);
  55. }
  56. return 0;
  57. }

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

代码:


  
  1. #include <iostream>
  2. using namespace std;
  3. 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 };
  4. int x[300][303];
  5. bool flag[300];
  6. int t, n, p;
  7. long long a;
  8. int main()
  9. {
  10. cin >> t;
  11. for (int i = 1; i <= t; i++)
  12. {
  13. cin >> n;
  14. int row = 0;
  15. while (n--)
  16. {
  17. cin >> a;
  18. for (int i = 0; i < 303; i++)
  19. {
  20. p = prime[i];
  21. while (a % (p*p) == 0)a /= (p*p);
  22. x[row][i] = (a%p == 0);
  23. }
  24. flag[row] = true;
  25. row++;
  26. }
  27. for (int i = 0; i < 303; i++)for (int j = 0; j < row; j++)if (flag[j] && x[j][i])
  28. {
  29. for (int k = j + 1; k < row; k++)if (flag[k] && x[k][i])
  30. for (int xi = 0; xi < 303; xi++)x[k][xi] ^= x[j][xi];
  31. flag[j] = false;
  32. break;
  33. }
  34. int r = 1, ji;
  35. for (int i = 0; i < row; i++)
  36. {
  37. ji = 0;
  38. if (flag[i])ji = 1;
  39. for (int j = 0; j < row; j++)if (x[i][j])ji = 0;
  40. if (ji)r = r * 2 % 1000000007;
  41. }
  42. printf("Case #%d:\n%d\n", i, (r - 1) % 1000000007);
  43. }
  44. return 0;
  45. }

UVA - 11542 Square

 

题目:

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

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

代码:


  
  1. #include <iostream>
  2. using namespace std;
  3. 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 };
  4. int x[300][303];
  5. bool flag[300];
  6. int t, n, p;
  7. long long a;
  8. int main()
  9. {
  10. cin >> t;
  11. for (int i = 1; i <= t; i++)
  12. {
  13. cin >> n;
  14. int row = 0;
  15. while (n--)
  16. {
  17. cin >> a;
  18. for (int i = 0; i < 303; i++)
  19. {
  20. p = prime[i];
  21. while (a % (p*p) == 0)a /= (p*p);
  22. x[row][i] = (a%p == 0);
  23. }
  24. flag[row] = true;
  25. row++;
  26. }
  27. for (int i = 0; i < 303; i++)for (int j = 0; j < row; j++)if (flag[j] && x[j][i])
  28. {
  29. for (int k = j + 1; k < row; k++)if (flag[k] && x[k][i])
  30. for (int xi = 0; xi < 303; xi++)x[k][xi] ^= x[j][xi];
  31. flag[j] = false;
  32. break;
  33. }
  34. long long r = 1, ji;
  35. for (int i = 0; i < row; i++)
  36. {
  37. ji = 0;
  38. if (flag[i])ji = 1;
  39. for (int j = 0; j < row; j++)if (x[i][j])ji = 0;
  40. if (ji)r = r * 2 ;
  41. }
  42. cout<<r-1<<endl;
  43. }
  44. return 0;
  45. }

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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