2026.3.14习题总结

· · 个人记录

P1146 硬币翻转

题目描述

在桌面上有一排硬币,共 N 枚,每一枚硬币均为正面朝上。现在要把所有的硬币翻转成反面朝上,规则是每次可翻转任意 N-1 枚硬币(正面向上的被翻转为反面向上,反之亦然)。求一个最短的操作序列(将每次翻转 N-1 枚硬币成为一次操作)。

数据范围:N 为不大于 100 的偶数

思路

经过多组数据可以得出,最少的次数是 n 次。接着我们可以发现第 i-1 行的第 j 个和第 i 行的第 j 个是相同的。
这是因为题目说:

对于有多种操作方案的情况,则只需操作的字典序最小输出一种。

正确代码:

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
bool vis[105];
int main() {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    cout<<n<<'\n';
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(j!=i) vis[j]=(!vis[j]);
            cout<<vis[j];
        }
        cout<<'\n';
    }
    return 0;
}
/*
4

4
0111
1100
0001
1111

*/
//大:localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'
//小:localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6'

P1611 循环的数字

题目描述

两个不同的正整数 (n, m) 是循环的,当且仅当你能通过将 n 末端的几个数字移到它的首端而不改变移动的数字的顺序并使整个数字变成 m 。举个例子,(12345, 34512) 就是一对循环的数字,因为你能把 12345 中末尾的 345 移到 12 前面,从而得到 34512。注意,为了成为一对循环的数字,n 和 m 位数必须相同。无论 n 或 m 都没有前置的 0。

现在给定正整数 A 和 B,并保证 A 和 B 位数相同且均没有前置 0,求存在多少循环的正整数对 (n, m),使得 A \leq n \le m \leq B ?

数据范围:1\le A,B \leq 2\times 10^6。

思路:

这一题没有什么太复杂的思路

重要部分:

for(int i=a;i<b;i++){
    n=i;
    m=(n%10)*num[cnt-1]+n/10;
    while(n!=m){
        ans+=n<m&&m<=b;
        m=(m%10)*num[cnt-1]+m/10;
    }
}

正确代码:

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
int num[10]={1,10,100,1000,10000,100000,1000000};
int main() {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a,b,n,m,cnt=0,pre,ans=0;
    cin>>a>>b;
    pre=a;
    while(pre){
        pre/=10;
        cnt++;
    }
    for(int i=a;i<b;i++){
        n=i;
        m=(n%10)*num[cnt-1]+n/10;
        while(n!=m){
            ans+=n<m&&m<=b;
            m=(m%10)*num[cnt-1]+m/10;
        }
    }
    cout<<ans;
    return 0;
}
/*

*/
//大:localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'
//小:localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6'