3 条题解

  • 2
    @ 2023-3-21 20:31:02

    #include <bits/stdc++.h> using namespace std; const int N = 100; int n,cur,q[N],st[N],isp[N]; void print() { ++cur; for (int i=1;i<=n;++i) cout<<q[i]<<' '; cout<<endl; } bool check(int k) { for(int i=2;i<=sqrt(k);++i) if(k%i==0) return false; return true; }

    void dfs(int k) { int i; if (cur>=10) return; if (k<=n) { if(k&1)i=3; else i=2; for(;i<=n;i+=2) { if (!st[i]) { st[i]=1; q[k]=i; if(isp[q[k-1]+q[k]])dfs(k+1); st[i]=0; } } } else if (isp[1+q[k-1]]) print(); } int main() { cin>>n; if (n&1) return 0; for(int i=2;i<=2*n;++i) isp[i]=check(i); q[1]=1; dfs(2); return 0; }

    • 1
      @ 2022-10-5 11:25:36

      给点个赞呗, 保证是AC ,语言C++98

      #include <bits/stdc++.h>
      using namespace std;
      const int N = 100;
      int n, cur, q[N], st[N], isp[N];
      void print()
      {
      	++ cur;
      	for (int i = 1; i <= n; ++ i)
      		cout << q[i] << ' ';
      	cout << endl;
      }
      
      bool check(int k)
      {
      	for (int i = 2; i <= sqrt(k); ++ i)
      		if (k % i == 0) return false;
      	return true;
      }
      
      void dfs(int k)
      {
      	int i;
      	if (cur >= 10) return;
      	if (k <= n) {
      		if (k & 1) i = 3;
      		else i = 2;
      		for (; i <= n; i += 2) {
      			if (!st[i]) {
      				st[i] = 1;
      				q[k] = i;
      				if (isp[q[k - 1] + q[k]])dfs(k + 1);
      				st[i] = 0;
      			}
      		}
      	} else if (isp[1 + q[k - 1]]) print();
      }
      
      int main()
      {
      	cin >> n;
      	if (n & 1) return 0;
      	for (int i = 2; i <= 2 * n; ++ i) isp[i] = check(i);
      	q[1] = 1;
      	dfs(2);
      	return 0;
      }
      
      
    • 0
      @ 2023-3-30 21:51:02

      • 1

      信息

      ID
      436
      时间
      1000ms
      内存
      128MiB
      难度
      9
      标签
      递交数
      332
      已通过
      31
      上传者