↓ Skip to main content
  1. Posts/

codeforces 679A A. Bear and Prime 100 (交互题,构造)

·560 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:存在一个[2..100]之间的数,每次可以询问一个数是否是该数的因子,返回yes或者no,最多询问20次。每次要输出询问的数,以及最后要输出这个数是否是质数。

思路:第一次做交互题。。。发现完全不能按照以前的思路。。。

更像是相反的。。。把output看做某种输入。。。input里是某种结果。。。我要根据input里的东西来确定一些东西。

就是先有output,再有input。。。output是选手的输入(最后一个除外),input是返回结果(不是你写的代码的返回结果)

对于这道题。。我们要尽可能少得猜一个数的因子,以确定该数是否为质数。

一个数不是质数的话,就有至少两个大于1的因子。。。

很容易想到。。。判素因子。。。

由于至少有2个非1的因子才不是素数。。。最小为2,因此另一个因子不会大于50.。。

此外。。。有可能有两个相同质因数组成的因子。。。。

因此还要判一下22,35,55,77

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :Sun 02 Oct 2016 08:22:44 PM CST
 4File Name :code/cf/problem/679A.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31int prime[19]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,4,9,25,49};
32string st;
33int main()
34{
35	#ifndef  ONLINE_JUDGE
36	freopen("code/in.txt","r",stdin);
37  #endif
38	int cnt = 0 ;
39	for ( int i = 0 ; i < 19 ; i++)
40	{
41	    cout<<prime[i]<<endl;
42	    fflush(stdout);
43	    cin>>st;
44	    if (st=="yes") cnt++;
45	}
46	if (cnt>=2) puts("composite");
47	else puts("prime");
48	fflush(stdout);
49  #ifndef ONLINE_JUDGE
50  fclose(stdin);
51  #endif
52    return 0;
53}

Related

codeforces 623 A. Graph and String (构造)

·708 words·2 mins
题目链接:题目链接 题意:给出一个无向图,该图是通过仅包含‘a’ ‘b’ ‘c’三个字母,以规则“i,j之间有边,当且仅当s[i]和s[j]相同,或者s[i]和s[j]在字母表中相邻”(也就是只有’a’和’c’是没有边相连的)得到的,现在问能否还原这个字符串,如果能,输出任意一个解。

codeforces #368 div 2 C. Pythagorean Triples (构造,数学)

·953 words·2 mins
题目链接 题意:给出一个数,问包含这个数三个数组成的勾股数,输出另外两个数。 思路: 所谓勾股数,就是当组成一个直角三角形的三边长都为正整数时,我们就称这一组数为勾股数. 那么,组成一组勾股数的三个正整数之间,是否具有一定的规律可寻呢?下面我们一起来观察几组勾股数: 规律一:在勾股数(3,4,5)、(5,12,13)、(7,24,25)(9,40,41)中,我们发现 由(3,4,5)有:32=9=4+5 由(5,12,13)有:52=25=12+13 由(7,24,25)有:72=49=24+25 由(9,40,41)有:92=81=40+41. 即在一组勾股数中,当最小边为奇数时,它的平方刚好等于另外两个连续的正整数之和.因此,我们把它推广到一般,从而可得出以下公式: ∵(2n+1)²=4n²+4n+1=(2n²+2n)+(2n²+2n+1) ∴(2n+1)²+(2n²+2n)²=(2n²+2n+1)²(n为正整数) 证明(略) 勾股数公式一:(2n+1,2n²+2n,2n²+2n+1)(n为正整数) 规律二:在勾股数(6,8,10)、(8,15,17)、(10,24,26)中,我们发现 由(6,8,10)有:62=36+2×(8+10) 由(8,15,17)有:82=64=2×(15+17) 由(10,24,26)有:102=100=2×(24+26) 即在一组勾股数中,当最小边为偶数时,它的平方刚好等于两个连续整数之和的二倍,推广到一般,从而可得出另一公式: ∵(2n)2=4n2=2[(n2-1)+(n2+1)] ∴(2n)2+(n2-1)2=(n2+1)2(n≥2且n为正整数) 证明(略) 勾股数公式二:(2n,n²-1,n²+1)(n≥2且n为正整数) 利用以上两个公式,我们可以快速写出各组勾股数.

codeforces 652 B. z-sort (简单构造)

·306 words·1 min
题目链接 题意:给出n个元素的序列,问能否得到一个新的序列,使得奇数位置非递减排列,偶数位数非递增排列。 思路:感觉一定可以啊。。。排序以后直接构造。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年03月31日 星期四 14时05分11秒 4File Name :code/cf/problem/652B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E3+7; 34int a[N]; 35int ans[N]; 36int n ; 37int main() 38{ 39 #ifndef ONLINE_JUDGE 40 freopen("code/in.txt","r",stdin); 41 #endif 42 43 cin>>n; 44 for ( int i = 1 ; i <= n ; i++) cin>>a[i]; 45 sort(a+1,a+n+1); 46 int cnt = 0 ; 47 for ( int i = 1 ; i <= n ; i+=2) 48 { 49 ans[i] = a[++cnt]; 50 } 51 52 for ( int i = n/2*2 ; i >= 1 ; i-=2) 53 { 54 ans[i] = a[++cnt]; 55 } 56 57 for ( int i = 1 ; i < n ; i++) cout<<ans[i]<<" "; 58 cout<<ans[n]<<endl; 59 60 #ifndef ONLINE_JUDGE 61 fclose(stdin); 62 #endif 63 return 0; 64}

codeforces #342 div 2 D. Finals in arithmetic

·555 words·2 mins
http://codeforces.com/contest/625/problem/D 题意:问能否找到一个s,满足s+s的反转=k 思路:如果是回文数。。。那么显然满足。除以2就可以得到答案。 代码实现 1 如果不是回文数。。那么考虑进位的情况。 2 要么从后一位进1,要么从前一位退10回来。 3 需要特殊考虑1开头的。 4 5 6 7/* *********************************************** 8Author :111qqz 9Created Time :2016年02月07日 星期日 18时28分39秒 10File Name :code/cf/#342/D.cpp 11************************************************ */ 12 13#include <cstdio> 14#include <cstring> 15#include <iostream> 16#include <algorithm> 17#include <vector> 18#include <queue> 19#include <set> 20#include <map> 21#include <string> 22#include <cmath> 23#include <cstdlib> 24#include <ctime> 25#include <sstream> 26#define fst first 27#define sec second 28#define lson l,m,rt<<1 29#define rson m+1,r,rt<<1|1 30#define ms(a,x) memset(a,x,sizeof(a)) 31typedef long long LL; 32#define pi pair < int ,int > 33#define MP make_pair 34 35using namespace std; 36const double eps = 1E-8; 37const int dx4[4]={1,0,0,-1}; 38const int dy4[4]={0,-1,1,0}; 39const int inf = 0x3f3f3f3f; 40const int N=1E5+7; 41bool vis[N]; 42 43void pre() 44{ 45 ms(vis,false); 46 for ( int i = 10 ; i <12000 ; i++) 47 { 48 int vala = i ; 49 stringstream ss; 50 ss<<vala; 51 string tmp = ss.str(); 52 53 reverse(tmp.begin(),tmp.end()); 54 55 int valb; 56 sscanf(tmp.c_str(),"%d",&valb); 57// cout<<i<<" "<<vala+valb<<endl; 58 if (vala+valb<N) vis[vala+valb] = true; 59 60 } 61 62 for ( int i = 1 ; i < N ; i++) 63 { 64 if (vis[i]) cout<<i<<endl; 65 } 66} 67char ans[N],s[N]; 68int n; 69int sum[N]; 70 71bool ok() 72{ 73 // cout<<"n:"<<n<<endl; 74 for ( int i = 0 ; i < n/2 ;) 75 { 76 int l = i ; 77 int r = n-1-i; 78 if (sum[l]==sum[r]) i++; 79 else if (sum[l]==sum[r]+1||sum[l]==sum[r]+11) 80 { 81 sum[l]--; 82 sum[l+1]+=10; 83 } 84 else if (sum[l]==sum[r]+10) 85 { 86 sum[r-1]--; 87 sum[r]+=10; 88 89 } 90 else return false; 91 } 92 93 if (n%2==1) 94 { 95 if (sum[n/2]%2==1) return false; 96 if (sum[n/2]>18||sum[n/2]<0) return false; 97 ans[n/2] = char(sum[n/2]/2 +'0'); 98// cout<<"ASDHJKASD"<<endl; 99 } 100 for ( int i = 0 ; i < n/2 ; i++) 101 { 102 if (sum[i]<0||sum[i]>18) return false; 103 ans[i] =(sum[i]+1)/2+'0'; 104 ans[n-1-i] =sum[i]/2+'0'; 105 } 106 return ans[0]>'0'; 107} 108int main() 109{ 110 #ifndef ONLINE_JUDGE 111 freopen("code/in.txt","r",stdin); 112 #endif 113 114 //pre(); 115 // 116 117 scanf("%s",s); 118 n = strlen(s); 119 for ( int i = 0 ; i < n ; i++) sum[i] = s[i] - '0'; 120 if (ok()) 121 { 122 cout<<ans<<endl; 123// cout<<"aaa"<<endl; 124 return 0; 125 } 126 127 if (s[0]=='1'&&n>1) 128 { 129 for ( int i = 0 ; i < n ; i++) 130 sum[i] = s[i+1]-'0'; 131 sum[0]+=10; 132 n--; 133 if (ok()) 134 { 135 cout<<ans<<endl; 136 return 0; 137 } 138 else 139 puts("0"); 140 } 141 else puts("0"); 142 143 144 #ifndef ONLINE_JUDGE 145 fclose(stdin); 146 #endif 147 return 0; 148}

codeforces #342 div 2 C. K-special Tables

·335 words·1 min
http://codeforces.com/contest/625/problem/C 题意:构造一个矩阵。。满足三个条件。。。 思路:简单构造。。。看代码把。。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月07日 星期日 17时49分15秒 4File Name :code/cf/#342/C.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=5E2+7; 34 35int n; 36int k; 37int ans[N][N]; 38int main() 39{ 40 #ifndef ONLINE_JUDGE 41 freopen("code/in.txt","r",stdin); 42 #endif 43 44 cin>>n>>k; 45 int cnt = 0; 46 ms(ans,0); 47 for ( int i = 1 ; i <= n ; i++) 48 for ( int j = 1 ; j <= k-1 ; j++) 49 { 50 cnt++; 51 ans[i][j] = cnt; 52 } 53 for ( int i = 1 ; i <= n ;i++) 54 for ( int j = k ; j <=n ;j++) 55 { 56 cnt++; 57 ans[i][j]=cnt; 58 } 59 int sum = 0 ; 60// cout<<"k:"<<k<<endl; 61 for ( int i = 1 ; i <= n ; i++) 62 { 63 sum +=ans[i][k]; 64 65 // cout<<"sum:"<<sum<<endl; 66 } 67 68 cout<<sum<<endl; 69 for ( int i = 1 ; i <= n ; i++) 70 { 71 for ( int j = 1 ; j <= n-1 ; j++) 72 { 73 printf("%d ",ans[i][j]); 74 } 75 printf("%d\n",ans[i][n]); 76 } 77 78 #ifndef ONLINE_JUDGE 79 fclose(stdin); 80 #endif 81 return 0; 82}