打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Codeforces Round #629 (Div. 3) A、B、C

传送门:点我

A:Divisibility Problem

大意:T组数据 给定a b ,a每次只能加一,问多少次操作后能让a%b==0

思路:如果a比b大,那么答案是(a/b 1)*b-a或者直接输出0(不用操作)

          如果a比b小,答案是b-a

代码:

#include<bits/stdc  .h> using namespace std;#define LL long long#define INF 2000000000#define eps 1e-8#define pi  3.141592653589793const LL mod = 1e9 7;using namespace std;int main() {    LL a,b;    int _;    for(scanf("%d",&_);_--;){        scanf("%lld %lld",&a,&b);        if(a<b){            printf("%d\n",b-a);        }else{            if(a%b == 0){                puts("0");            }else{                printf("%lld\n",(a/b 1)*b-a);            }        }            }}/* */ 

 

B:K-th Beautiful String

time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

For the given integer nn (n>2n>2) let's write down all the strings of length nn which contain n−2n−2 letters 'a' and two letters 'b' in lexicographical (alphabetical) order.

Recall that the string ss of length nn is lexicographically less than string tt of length nn, if there exists such ii (1≤i≤n1≤i≤n), that si<tisi<ti, and for any jj (1≤j<i1≤j<i) sj=tjsj=tj. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.

For example, if n=5n=5 the strings are (the order does matter):

  1. aaabb
  2. aabab
  3. aabba
  4. abaab
  5. ababa
  6. abbaa
  7. baaab
  8. baaba
  9. babaa
  10. bbaaa

It is easy to show that such a list of strings will contain exactly n⋅(n−1)2n⋅(n−1)2 strings.

You are given nn (n>2n>2) and kk (1≤k≤n⋅(n−1)21≤k≤n⋅(n−1)2). Print the kk-th string from the list.

Input

The input contains one or more test cases.

The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. Then tt test cases follow.

Each test case is written on the the separate line containing two integers nn and kk (3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2)3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2).

The sum of values nn over all test cases in the test doesn't exceed 105105.

Output

For each test case print the kk-th string from the list of all described above strings of length nn. Strings in the list are sorted lexicographically (alphabetically).

ExampleinputCopy
75 15 25 85 103 13 220 100
outputCopy
aaabbaababbaababbaaaabbbabaaaaabaaaaabaaaaaaaa

 

大意:

需要输出的字符串中有且必有2个b,其余都是a。

T组数据,给定n和k,询问有n个字母构成的字符串第k大的字符串是啥

思路:

观察到abb 是第1个  bab是第2个 baab是第4个 baaab是第7个,再手动计算了一下baaaab是第11个。

由此很容易的看得出来如果一个b在末尾,前一个b移动的规律是数组[1,2,3,4....]的前缀和。

也很好理解,因为每次都要加上后面一个b的活动范围。

那么就很好算得出第一个b的位置了,直接打表二分即可。算出第一个b的位置,第二个b也好算啦。

代码:

#include<bits/stdc  .h> using namespace std;#define LL long long#define INF 2000000000#define eps 1e-8#define pi  3.141592653589793const LL mod = 1e9 7;using namespace std;vector<LL>v;int main() {    LL ans = 1;    for(int i = 2 ; i <= 100000 ; i   ){        v.push_back(ans);        ans = ans (i-1);    }    LL a,b;    int _;    for(scanf("%d",&_);_--;){        scanf("%lld %lld",&a,&b);        int pos1 = lower_bound(v.begin(),v.end(),b)-v.begin(),pos2;//pos1是第一个b的位置,二分算得,pos2是第二个b的位置,由输入的k和pos1共同决定        if(b == v[pos1]){            pos2 = 0;            pos1  = 1;        }else{            pos2 = b-v[pos1-1];        }        string s = "";        for(int i = 0 ; i < a ; i   ){            s  = 'a';        }        s[a-pos2-1] = 'b';        s[a-pos1-1] = 'b';        cout<<s<<endl;    }}/*bb    1bab   2baab  4baaab 7 */ 

 

C:Ternary XOR

time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

A number is ternary if it contains only digits 00, 11 and 22. For example, the following numbers are ternary: 10221022, 1111, 2121, 20022002.

You are given a long ternary number xx. The first (leftmost) digit of xx is guaranteed to be 22, the other digits of xx can be 00, 11 or 22.

Let's define the ternary XOR operation ⊙⊙ of two ternary numbers aa and bb (both of length nn) as a number c=a⊙bc=a⊙b of length nn, where ci=(ai bi)

Your task is to find such ternary numbers aa and bb both of length nn and both without leading zeros that a⊙b=xa⊙b=x and max(a,b)max(a,b) is the minimum possible.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow. The first line of the test case contains one integer nn (1≤n≤5⋅1041≤n≤5⋅104) — the length of xx. The second line of the test case contains ternary number xx consisting of nn digits 0,10,1 or 22. It is guaranteed that the first digit of xx is 22. It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1045⋅104 (∑n≤5⋅104∑n≤5⋅104).

Output

For each test case, print the answer — two ternary integers aa and bb both of length nn and both without leading zeros such that a⊙b=xa⊙b=x and max(a,b)max(a,b) is the minimum possible. If there are several answers, you can print any.

ExampleinputCopy
4522222521211129220222021
outputCopy
1111111111110001021111110111011110111010

 

思路:对0,1,2的情况分类讨论即可。

代码:

#include<bits/stdc  .h> using namespace std;#define LL long long#define INF 2000000000#define eps 1e-8#define pi  3.141592653589793const LL mod = 1e9 7;using namespace std;int main() {    string s;    int _;    for(scanf("%d",&_);_--;){        cin>>s>>s;        string s1 = "",s2 = "";        int flag = 0;        for(int i = 0 ; i < s.size() ; i   ){            if(s[i] == '2'){                if(flag == 0){                    s1  = '1';                    s2  = '1';                }else{                    s1  = '0';                    s2  = '2';                }            }else if(s[i] == '1'){                if(flag == 0){                    s1  = '1';                    s2  = '0';                    flag = 1;                }else{                    s1  = '0';                    s2  = '1';                }             }else if(s[i] == '0'){                s1  = '0';                s2  = '0';            }        }        cout<<s1<<endl;        cout<<s2<<endl;    }}/*4522222521211129220222021  */ 

 

 

来源:https://www.icode9.com/content-4-667901.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
正方加密解密算法及获取密钥
Codeforces Round #653 (Div. 3)
PHP魔术方法示例
扶凯 ? [Perl] Getopt 函数来接收用户参数的使用
for each 循环+可变参数的小例子(JDK5)
浅谈js命名空间管理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服