P2241 统计方形(数据加强版)
1 #include<iostream>
2 using namespace std;
3 long long n,m,rec,sqr;
4 int main() {
5 cin>>n>>m;
6 for(int i=1; i<=n; i++)//循环一条边长
7 for(int j=1; j<=m; j++) {//循环另一条边长0
8 if(i==j) sqr+=(n-i+1)*(m-j+1);//如果i==j,说明是正方形
9 else rec+=(n-i+1)*(m-j+1);//如果不等说明是矩形
10 }
11 cout<<sqr<<" "<<rec<<endl;//输出
12 return 0;
13 }
P2089 烤鸡
1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 int a,b,c,d,e,f,g,h,i,j,in,x=0;
6 cin>>in;
7 for (a=1;a<=3;a++)
8 {
9 for (b=1;b<=3;b++)
10 {
11 for (c=1;c<=3;c++)
12 {
13 for (d=1;d<=3;d++)
14 {
15 for (e=1;e<=3;e++)
16 {
17 for (f=1;f<=3;f++)
18 {
19 for (g=1;g<=3;g++)
20 {
21 for(h=1;h<=3;h++)
22 {
23 for (i=1;i<=3;i++)
24 {
25 for (j=1;j<=3;j++)
26 {
27 if (a+b+c+d+e+f+g+h+i+j==in)
28 {
29 x++;
30 }
31 }
32 }
33 }
34 }
35 }
36 }
37 }
38 }
39 }
40 }
41 cout<<x<<endl;
42 for (a=1;a<=3;a++)
43 {
44 for (b=1;b<=3;b++)
45 {
46 for (c=1;c<=3;c++)
47 {
48 for (d=1;d<=3;d++)
49 {
50 for (e=1;e<=3;e++)
51 {
52 for (f=1;f<=3;f++)
53 {
54 for (g=1;g<=3;g++)
55 {
56 for(h=1;h<=3;h++)
57 {
58 for (i=1;i<=3;i++)
59 {
60 for (j=1;j<=3;j++)
61 {
62 if (a+b+c+d+e+f+g+h+i+j==in)
63 {
64 cout<<a<<" ";
65 cout<<b<<" ";
66 cout<<c<<" ";
67 cout<<d<<" ";
68 cout<<e<<" ";
69 cout<<f<<" ";
70 cout<<g<<" ";
71 cout<<h<<" ";
72 cout<<i<<" ";
73 cout<<j<<endl;
74 }
75 }
76 }
77 }
78 }
79 }
80 }
81 }
82 }
83 }
84 }
85 return 0;
86 }
P1618 三连击(升级版)
1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 int a,b,c;
5 int num[15];
6 int n=0;
7 bool work (int t) {
8 int ge=t%10;
9 int shi=t/10%10;
10 int bai=t/100;
11
12 if(num[ge]==0&&ge!=0) num[ge]=1;
13 else return false;
14
15 if(num[shi]==0&&shi!=0) num[shi]=1;
16 else return false;
17
18 if(num[bai]==0&&bai!=0) num[bai]=1;
19 else return false;
20 return true;
21 }
22
23 int main() {
24 cin>>a>>b>>c;
25 if(a==0||b==0||c==0) {
26 cout<<"No!!!";
27 return 0;
28 }
29 int l=100/a+1;
30 for(int i=l; i; i++) { //a:b:c的i倍 保证比例正确
31 memset(num,0,sizeof(num));
32 int x=a*i;
33 int y=b*i;
34 int z=c*i;
35 //cout<<x<<' '<<y<<' '<<z<<endl;
36 //在三位数范围内 a<b<c
37 if(x>100&&z<1000) {
38 //验证三个数的每一位
39 if(work(x)&&work(y)&&work(z)) {
40 cout<<x<<' '<<y<<' '<<z<<endl;
41 n++;
42 }
43 }
44 if(z>1000) break;
45 }
46 if(n==0) {
47 cout<<"No!!!";
48 }
49 return 0;
50 }
P1036 [NOIP2002 普及组] 选数
1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 const int N=25;
5 int a[N],n,k;
6 int sum=0;
7 int cot(int x) { //返回x中有几位是1,sum保存取到数的和
8 sum=0;
9 int t=0,idx=0;
10 while(x>0) {
11 idx++;//记录运行到哪一位了
12 if(x&1==1) {
13 t++;
14 sum+=a[idx];
15 }
16 x>>=1;
17 }
18 return t;
19 }
20
21 bool is_prime(int x) {
22 if(x<=1) return false;
23 for(int i=2; i*i<=x; i++) {
24 if(x%i==0) return false;
25 }
26 return true;
27 }
28
29 int main() {
30 cin>>n>>k;
31 for(int i=1; i<=n; i++) cin>>a[i];
32 int s=(1<<n)-1;
33 int ans=0;
34 for(int i=1; i<=s; i++) { //每一个数转化为二进制表示一种状态
35 //1表示取,0表示不取
36 if(cot(i)==k&&is_prime(sum)) { //验证取了几个数&& 和是素数
37 ans++;
38 }
39 }
40 cout<<ans;
41 return 0;
42 }
P1157 组合的输出
1 #include<iostream>
2 #include<cstring>
3 #include<iomanip>
4 using namespace std;
5 int n,r;
6 int ans[25];
7 int cot(int x) { //返回x中有几位是1
8 int t=0;
9 int idx=0;
10 while(x>0) {
11 idx++;
12 if(x&1==1) {
13 t++;
14 ans[t]=n-idx+1;
15 }
16 x>>=1;
17 }
18 return t;
19 }
20
21 int main() {
22 cin>>n>>r;
23 int s=(1<<n)-1;
24 for(int i=s; i>=0; i--) {
25 int k=cot(i);
26 if(k==r) {
27 for(int j=k; j>=1; j--)
28 cout<<setw(3)<<ans[j];
29 cout<<endl;
30 }
31 }
32 return 0;
33 }
P1706 全排列问题
1 //P1706 全排列问题
2 #include<iostream>
3 #include<iomanip>
4 using namespace std;
5 const int N=10;
6 int n;
7 int a[N];
8 int main() {
9 cin>>n;
10 if(n==1) {
11 cout<<setw(5)<<1<<endl;
12 } else {
13 for(int n1=1; n1<=n; n1++) {
14 a[n1]=1;
15 for(int n2=1; n2<=n; n2++) {
16 if(a[n2]==0) a[n2]=1;
17 else continue;
18 if(n==2) {
19 cout<<setw(5)<<n1<<setw(5)<<n2<<endl;
20 a[n2]=0;
21 continue;
22 }
23 for(int n3=1; n3<=n; n3++) {
24 if(a[n3]==0) a[n3]=1;
25 else continue;
26 if(n==3) {
27 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<endl;
28 a[n3]=0;
29 continue;
30 }
31 for(int n4=1; n4<=n; n4++) {
32 if(a[n4]==0) a[n4]=1;
33 else continue;
34 if(n==4) {
35 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<setw(5)<<n4<<endl;
36 a[n4]=0;
37 continue;
38 }
39 for(int n5=1; n5<=n; n5++) {
40 if(a[n5]==0) a[n5]=1;
41 else continue;
42 if(n==5) {
43 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<setw(5)<<n4<<setw(5)<<n5<<endl;
44 a[n5]=0;
45 continue;
46 }
47 for(int n6=1; n6<=n; n6++) {
48 if(a[n6]==0) a[n6]=1;
49 else continue;
50 if(n==6) {
51 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<setw(5)<<n4<<setw(5)<<n5<<setw(5)<<n6<<endl;
52 a[n6]=0;
53 continue;
54 }
55 for(int n7=1; n7<=n; n7++) {
56 if(a[n7]==0) a[n7]=1;
57 else continue;
58 if(n==7) {
59 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<setw(5)<<n4<<setw(5)<<n5<<setw(5)<<n6<<setw(5)<<n7<<endl;
60 a[n7]=0;
61 continue;
62 }
63 for(int n8=1; n8<=n; n8++) {
64 if(a[n8]==0) a[n8]=1;
65 else continue;
66 if(n==8) {
67 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<setw(5)<<n4<<setw(5)<<n5<<setw(5)<<n6<<setw(5)<<n7<<setw(5)<<n8<<endl;
68 a[n8]=0;
69 continue;
70 }
71 for(int n9=1; n9<=n; n9++) {
72 if(a[n9]==0) a[n9]=1;
73 else continue;
74 if(n==9) {
75 cout<<setw(5)<<n1<<setw(5)<<n2<<setw(5)<<n3<<setw(5)<<n4<<setw(5)<<n5<<setw(5)<<n6<<setw(5)<<n7<<setw(5)<<n8<<setw(5)<<n9<<endl;
76 a[n9]=0;
77 continue;
78 }
79 a[n9]=0;
80 }
81 a[n8]=0;
82 }
83 a[n7]=0;
84 }
85 a[n6]=0;
86 }
87 a[n5]=0;
88 }
89 a[n4]=0;
90 }
91 a[n3]=0;
92 }
93 a[n2]=0;
94 }
95 a[n1]=0;
96 }
97 }
98 return 0;
99 }
P3392 涂国旗
1 #include<iostream>
2 #include<cstring>
3 #include<cmath>
4 using namespace std;
5 const int N=55;
6 int n,m;
7 int a[N][N];
8 int b[N][5];// 记录每一行有几个这种颜色
9 int main() {
10 cin>>n>>m;
11 for(int i=1; i<=n; i++) {
12 for(int j=1; j<=m; j++) {
13 char c;
14 cin>>c;
15 if(c=='W') {
16 a[i][j]=1;//w标记为1
17 b[i][1]++;
18 } else if(c=='B') {
19 a[i][j]=2;//b标记为2
20 b[i][2]++;
21 } else {
22 a[i][j]=3;//b标记为3
23 b[i][3]++;
24 }
25 }
26 }
27 int ans=n*m;
28 for(int i=2; i<=n-1; i++) { //标记第一次蓝色出现在哪一行
29 for(int j=3; j<=n; j++) { //标记第一次红色出现在哪一行
30 int sum=0;
31 for(int x=1; x<i; x++) {//1到i-1改成白色
32 sum+=b[x][2]+b[x][3];
33 }
34 for(int x=i; x<j; x++) { //i到j-1改成蓝色
35 sum+=b[x][1]+b[x][3];
36 }
37 for(int x=j; x<=n; x++) {//j到n改成红色
38 sum+=b[x][1]+b[x][2];
39 }
40 ans=min(ans,sum);
41 }
42
43 }
44 cout<<ans;
45 return 0;
46 }
P3654 First Step (ファーストステップ)
1 #include<iostream>
2 using namespace std;
3 char a[105][105];//地图
4 long long r,c,k;
5 long long ans=0;//输出
6 int main()
7 {
8 cin >> r >> c >> k ;
9 for(int i=1;i<=r;i++)
10 {
11 for(int j=1;j<=c;j++)
12 {
13 cin >> a[i][j] ;
14 }
15 }
16 bool f=true;
17 for(int i=1;i<=r;i++)//横向搜索
18 {
19 for(int j=1;j<=c;j++)
20 {
21 f=true;
22 for(int s=0;s<k;s++)
23 {
24 if(a[i+s][j]!='.')
25 {
26 f=false;
27 break;
28 }
29 }
30 if(f==true)
31 {
32 ans++;
33 }
34 }
35 }
36 for(int i=1;i<=r;i++)//纵向搜索
37 {
38 for(int j=1;j<=c;j++)
39 {
40 f=true;
41 for(int s=0;s<k;s++)
42 {
43 if(a[i][j+s]!='.')
44 {
45 f=false;
46 break;
47 }
48 }
49 if(f==true)
50 {
51 ans++;
52 }
53 }
54 }
55 if(k==1)//特判
56 {
57 cout << ans/2 ;
58 }else
59 {
60 cout << ans ;
61 }
62 return 0;
63 }
P1217 [USACO1.5]回文质数 Prime Palindromes
1 #include<iostream>
2 #include<cstring>
3 #include<cmath>
4 using namespace std;
5 int a,b;
6
7 bool is_prime(int x) {
8 if(x<=1) return false;
9 for(int i=2; i*i<=x; i++) {
10 if(x%i==0) return false;
11 }
12 return true;
13 }
14
15 int main() {
16 cin>>a>>b;
17 //处理一和两位
18 if(a<=5&&b>=5) cout<<5<<endl;
19 if(a<=7&&b>=7) cout<<7<<endl;
20 if(a<=11&&b>=11) cout<<11<<endl;
21 //三位
22 for(int i=1; i<=9; i++) {
23 for(int j=0; j<=9; j++) {
24 int x=i*100+j*10+i;
25 if(is_prime(x)&&x>=a&&x<=b) cout<<x<<endl;
26 else {
27 if(x>=b) return 0;
28 }
29 }
30 }
31 //四位 然后你会发现没有满足条件的四位数
32 for(int i=1; i<=9; i++) {
33 for(int j=0; j<=9; j++) {
34 int x=i*1000+j*100+j*10+i;
35 if(is_prime(x)&&x>=a&&x<=b) cout<<x<<endl;
36 else {
37 if(x>=b) return 0;
38 }
39 }
40 }
41 //五位
42 for(int i=1; i<=9; i++) {
43 for(int j=0; j<=9; j++) {
44 for(int k=0; k<=9; k++) {
45 int x=i*10000+j*1000+k*100+j*10+i;
46 if(is_prime(x)&&x>=a&&x<=b) cout<<x<<endl;
47 else {
48 if(x>=b) return 0;
49 }
50 }
51 }
52 }
53 //六位 然后你会发现没有满足条件的六位数
54 for(int i=1; i<=9; i++) {
55 for(int j=0; j<=9; j++) {
56 for(int k=0; k<=9; k++) {
57 int x=i*100000+j*10000+k*1000+k*100+j*10+i;
58 if(is_prime(x)&&x>=a&&x<=b) cout<<x<<endl;
59 else {
60 if(x>=b) return 0;
61 }
62 }
63 }
64 }
65 //七位
66 for(int i=1; i<=9; i++) {
67 for(int j=0; j<=9; j++) {
68 for(int k=0; k<=9; k++) {
69 for(int u=0; u<=9; u++) {
70 int x=i*1000000+j*100000+k*10000+u*1000+k*100+j*10+i;
71 if(is_prime(x)&&x>=a&&x<=b) cout<<x<<endl;
72 else {
73 if(x>=b) return 0;
74 }
75 }
76 }
77 }
78 }
79 //八位 然后你会发现没有满足条件的八位数
80 for(int i=1; i<=9; i++) {
81 for(int j=0; j<=9; j++) {
82 for(int k=0; k<=9; k++) {
83 for(int u=0; u<=9; u++) {
84 int x=i*10000000+j*1000000+k*100000+u*10000+u*1000+k*100+j*10+i;
85 if(is_prime(x)&&x>=a&&x<=b) cout<<x<<endl;
86 else {
87 if(x>=b) return 0;
88 }
89 }
90 }
91 }
92 }
93 return 0;
94 }
1 //P1217 [USACO1.5]回文质数 Prime Palindromes
2 #include<iostream>
3 #include<cstdio>
4 #include<cmath>
5 using namespace std;
6
7 bool is_prime(int x) {
8 if (x<=1) return false;
9 else if (x==2) return true;
10 else {
11 int a=sqrt(x);
12 for(int i=2; i<a+1; i++) {
13 if(x%i==0) return false;
14 }
15 return true;
16 }
17 }
18 bool isHWS(int num) {
19 int temp=num,ans=0;
20 while (temp!=0) {
21 ans=ans*10+temp%10;
22 temp/=10;
23 }
24 if (ans==num)
25 return true;
26 else
27 return false;
28 }
29
30 int main() {
31 int a,b;
32 scanf("%d%d",&a,&b);
33 for(int i=a; i<=b; i++) {
34 if(i>9989899) break;
35 if(isHWS(i)&&is_prime(i)) {
36 printf("%d\n",i);
37 }
38 }
39 return 0;
40 }
P1149 [NOIP2008 提高组] 火柴棒等式
1 #include<iostream>
2 using namespace std;
3
4 int n;
5 int a[10]= {6,2,5,5,4,5,6,3,7,6};
6 int sum(int x) {
7 if(x==0) return a[0];
8 int s=0;
9 while(x>0) {
10 int k=x%10;
11 s+=a[k];
12 x/=10;
13 }
14 return s;
15 }
16 int main() {
17 cin>>n;
18 n-=4;
19 int ans=0;
20 for(int i=0; i<=1111; i++) {
21 for(int j=i; j<=1111; j++) {
22 int t=sum(i)+sum(j)+sum(i+j);
23 if(t==n) {
24 if(i==j) ans++;
25 else ans+=2;
26 }
27 }
28 }
29 cout<<ans;
30 return 0;
31 }
P3799 妖梦拼木棒
1 #include<iostream>
2 #include<cmath>
3 using namespace std;
4 const int N=100010;
5 const int mod=1000000007;
6 int n;
7 int mmax=0;
8 int num[5010];//桶
9 int main() {
10 //freopen("P3799_1.in","r",stdin);
11 cin>>n;
12 for(int i=1; i<=n; i++) {
13 int t;
14 cin>>t;
15 num[t]++;
16 mmax=max(t,mmax);//记录最大边长
17 }
18 long long ans=0;
19 for(int i=1; i<=mmax; i++) {//两根一样长的
20 if(num[i]<2) continue;
21 long long t=num[i]*(num[i]-1)/2%mod;
22 for(int j=1; j<=i/2; j++) {//一根拼接的
23 if(num[j]==0||num[i-j]==0) continue;
24 if(i-j!=j) {
25 ans=(ans+num[j]*num[i-j]%mod*t%mod)%mod;
26 } else {
27 ans=(ans+num[j]*(num[j]-1)/2%mod*t%mod)%mod;
28 }
29 //cout<<i<<' '<<j<<' '<<t<<" "<<ans<<endl;
30 }
31 }
32 cout<<ans;
33 return 0;
34 }