#include<bits/stdc++.h> using namespace std; const int MAXN=50; int a[MAXN][MAXN];//增广矩阵 int x[MAXN];//解集 bool free_x[MAXN];//标记是否是不确定的变元 int gcd(int a,int b){ if(b == 0) return a; else return gcd(b,a%b);} inline int lcm(int a,int b){return a/gcd(a,b)*b;}//先除后乘防溢出} // 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解, //-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数) //有equ个方程,var个变元。增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var. int Gauss(int equ,int var) { int i,j,k; int max_r;// 当前这列绝对值最大的行. int col;//当前处理的列 int ta,tb; int LCM; int temp; int free_x_num; int free_index; for(int i=0;i<=var;i++){ x[i]=0; free_x[i]=true; } //转换为阶梯阵. col=0; // 当前处理的列 for(k = 0;k < equ && col < var;k++,col++){// 枚举当前处理的行. // 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差) max_r=k; for(i=k+1;i<equ;i++){ if(abs(a[i][col])>abs(a[max_r][col])) max_r=i; } if(max_r!=k){// 与第k行交换. for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]); } if(a[k][col]==0){// 说明该col列第k行以下全是0了,则处理当前行的下一列. k--; continue; } for(i=k+1;i<equ;i++){// 枚举要删去的行. if(a[i][col]!=0){ LCM = lcm(abs(a[i][col]),abs(a[k][col])); ta = LCM/abs(a[i][col]); tb = LCM/abs(a[k][col]); if(a[i][col]*a[k][col]<0)tb=-tb;//异号的情况是相加 for(j=col;j<var+1;j++){ a[i][j] = a[i][j]*ta-a[k][j]*tb; } } } } // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0). for (i = k; i < equ; i++){ // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换. if (a[i][col] != 0) return -1; } // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵. // 且出现的行数即为自由变元的个数. if (k < var){ return var - k; // 自由变元有var - k个. } // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵. // 计算出Xn-1, Xn-2 ... X0. for (i = var - 1; i >= 0; i--){ temp = a[i][var]; for (j = i + 1; j < var; j++){ if (a[i][j] != 0) temp -= a[i][j] * x[j]; } if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解. x[i] = temp / a[i][i]; } return 0; } int main(void){ // freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); int i, j; int equ,var; while (scanf("%d %d", &equ, &var) != EOF){ memset(a, 0, sizeof(a)); for (i = 0; i < equ; i++){ for (j = 0; j < var + 1; j++){ scanf("%d", &a[i][j]); } } int free_num = Gauss(equ,var); if (free_num == -1) printf("无解!\n"); else if (free_num == -2) printf("有浮点数解,无整数解!\n"); else if (free_num > 0){ printf("无穷多解! 自由变元个数为%d\n", free_num); for (i = 0; i < var; i++){ if (free_x[i]) printf("x%d 是不确定的\n", i + 1); else printf("x%d: %d\n", i + 1, x[i]); } }else{ for (i = 0; i < var; i++){ printf("x%d: %d\n", i + 1, x[i]); } } printf("\n"); } return 0; }
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define r register #define ll long long using namespace std; const int N = 1e6 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e6 + 3; const int p = 1e6 + 3; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n = 11; LL a[20][20], ans[N], maxi, tmp; ll ksm(r ll x,r int y) { if(!y) return 1; r ll ret=ksm(x,y>>1); if(y&1) return ret*ret%p*x%p; return ret*ret%p; } int Ask(int x) { printf("? %d\n", x); fflush(stdout); int y; scanf("%d", &y); return y;} // 偷的 int main() { for(int i = 1; i <= n; i++) { LL val = Ask(i - 1); for(int j = 1; j <= n; j++) a[i][j] = ksm(i - 1, j - 1); a[i][n + 1] = val; } for(r int i=1;i<=n;i++) { if(!a[i][i])//主元不能为0 { maxi=0; for(r int j=i+1;j<=n&&!maxi;j++) if(a[j][i]) maxi=j; if(!maxi) continue;//如果一整列都为0,不需要消元 for(r int j=i;j<=n+1;j++) tmp=a[maxi][j],a[maxi][j]=a[i][j],a[i][j]=tmp; } for(r int j=i+1;j<=n;j++) { tmp=a[j][i]; if(!tmp) continue;//已经为0,不需要消元 for(r int k=i;k<=n+1;k++) a[j][k]=((a[j][k]*a[i][i]-a[i][k]*tmp)%p+p)%p; } } for(r int i=n;i;i--) { for(r int j=i+1;j<=n;j++) a[i][n+1]=((a[i][n+1]-ans[j]*a[i][j])%p+p)%p; ans[i]=a[i][n+1]*ksm(a[i][i],p-2)%p; } for(int i = 0; i < p; i++) { LL ret = 0; for(int j = 0; j < 11; j++) ret = (ret + ksm(i, j) * ans[j + 1] % mod) % mod; if(ret==0) { printf("! %d\n", i); fflush(stdout); return 0; } } puts("! -1"); fflush(stdout); return 0; } #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define r register #define ll long long using namespace std; const int N = 1e6 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e6 + 3; const int p = 1e6 + 3; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n = 11; LL a[20][20], ans[N], maxi, tmp; ll ksm(r ll x,r int y) { if(!y) return 1; r ll ret=ksm(x,y>>1); if(y&1) return ret*ret%p*x%p; return ret*ret%p; } int Ask(int x) { printf("? %d\n", x); fflush(stdout); int y; scanf("%d", &y); return y;} // 偷的 int main() { for(int i = 1; i <= n; i++) { LL val = Ask(i - 1); for(int j = 1; j <= n; j++) a[i][j] = ksm(i - 1, j - 1); a[i][n + 1] = val; } for(r int i=1;i<=n;i++) { if(!a[i][i])//主元不能为0 { maxi=0; for(r int j=i+1;j<=n&&!maxi;j++) if(a[j][i]) maxi=j; if(!maxi) continue;//如果一整列都为0,不需要消元 for(r int j=i;j<=n+1;j++) tmp=a[maxi][j],a[maxi][j]=a[i][j],a[i][j]=tmp; } for(r int j=i+1;j<=n;j++) { tmp=a[j][i]; if(!tmp) continue;//已经为0,不需要消元 for(r int k=i;k<=n+1;k++) a[j][k]=((a[j][k]*a[i][i]-a[i][k]*tmp)%p+p)%p; } } for(r int i=n;i;i--) { for(r int j=i+1;j<=n;j++) a[i][n+1]=((a[i][n+1]-ans[j]*a[i][j])%p+p)%p; ans[i]=a[i][n+1]*ksm(a[i][i],p-2)%p; } for(int i = 0; i < p; i++) { LL ret = 0; for(int j = 0; j < 11; j++) ret = (ret + ksm(i, j) * ans[j + 1] % mod) % mod; if(ret==0) { printf("! %d\n", i); fflush(stdout); return 0; } } puts("! -1"); fflush(stdout); return 0; }