CF852A Digits

隔壁yijian大佬写出了正解。那我就写一个随机化大法吧?

我们先考虑一种错误的贪心,每个数字分成一位,使其分割后数字和最小。虽然这样是错的,但是我们发现错误的概率很小,所以我们可以每次随机一个数字一位或者两个数字一位。后者的概率调整在百分之一左右。我们用这样的方法做出第一次分割,剩余的两次分割都每个数字一位即可。最后判断一下是否满足条件,如果不满足就重新跑一遍随机化。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<time.h>
#include<string>
#include<stdlib.h>
#define MAX 100
using namespace std;
char s[200500];
int ans[200500],cnt=0,n,nums;
int data[3][200500],add[3];
string output;
int rd() {
int num=rand()%MAX;
if(num==0)return 2;
else return 1;
}
bool generate() {
cnt=0,nums=0;output.clear();
for(int i=0;i<n;i++) {
if(i!=0)output.push_back('+');
if(i!=n-1&&rd()==2) {
ans[++cnt]=(s[i]-'0')*10+s[i+1]-'0';
output.push_back(s[i]);output.push_back(s[i+1]);
i++;
}
else {
ans[++cnt]=s[i]-'0';
output.push_back(s[i]);
}
}
return 1;
}
void out() {
cout<<output;
printf("\n");
for (int i = add[1]; i >=1; i--) {
printf("%d", data[1][i]);
if (i != 1)printf("+");
}
printf("\n");
for (int i = add[2]; i >=1; i--) {
printf("%d", data[2][i]);
if (i != 1)printf("+");
}
return;
}
int main() {
srand((unsigned)(time(NULL)));
scanf("%d%s",&n,s);
while(generate()) {
long long tot=0,trans;add[0]=add[1]=add[2]=0;
for(int i=1;i<=cnt;i++)tot+=ans[i];trans=tot;tot=0;
while(trans){tot+=trans%10;data[1][++add[1]]=trans%10;trans/=10;}trans=tot;tot=0;
while(trans){tot+=trans%10;data[2][++add[2]]=trans%10;trans/=10;}
if(tot<=9){out();return 0;}
else continue;
}
}

CF852A Digits的更多相关文章

  1. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  2. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  4. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. Revolving Digits[EXKMP]

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  9. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

随机推荐

  1. 解决mac/win双系统,mac原生读写NTFS分区重启后失效的问题

    安装mac/win双系统,然后在mac下启用原生的NTFS分区读写功能,并将分区创建桌面快捷方式后,会发现有时候进入win后再进mac,原来创建的分区桌面快捷方式是白色的图标,并且分区也无法打开,这个 ...

  2. Java-Iterator遍历集合

    import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Ite ...

  3. 快捷键-Visual Studio Code快捷键

    Shift+Enter 在Python终端中运行选定内容/行 C

  4. rabbitMQ基础应用

    1.安装erlang [root@localhost ~]#yum -y install erlang 2.安装rabbitMQ [root@localhost ~]#yum -y install r ...

  5. Centos7安装pip或pip3

    1.使用Python2安装pip wget wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip- ...

  6. 【转】WPF 异步执行方法后对 UI 进行更新的几种方法

    使用 async/await 的情况: private async void Button_Click(object sender, RoutedEventArgs e) { (sender as B ...

  7. Linux计划任务管理

    计划任务 类型:     一次性计划任务     周期性计划任务      一次性计划任务 前提:  atd服务必须运行 [root@wei init.d]# yum -y install at   ...

  8. linux部署docker镜像

    安装git yum install git 生成ssh秘钥 cat ~/.ssh/id_rsa.pub //查看是否有秘钥 ssh-keygen -t rsa -C "" //生成 ...

  9. python 和 R 语言中的等差数列

    等差数列的通项公式:an = a0 + n*d. 数学上 n 是可以取遍整个整个正整数集的,在现实中,n  是有范围的. 1.R 语言用 seq()  函数产生等差数列: 2.python 中 ran ...

  10. 用js怎么来判断我已点击了窗体中“关闭”按钮?

    onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发. 该事件可用于弹出对话框,提示用户是继续浏览页面还是离开当前页面. 对话框默认的提示信息根据不同的浏览器有所不同,标准的信息类 ...