【Codeforces 1422 C】Bargain,dp方案书,组合分类计算贡献

problem

C. Bargain
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Sometimes it is not easy to come to an agreement in a bargain. Right now Sasha and Vova can’t come to an agreement: Sasha names a price as high as possible, then Vova wants to remove as many digits from the price as possible. In more details, Sasha names some integer price n, Vova removes a non-empty substring of (consecutive) digits from the price, the remaining digits close the gap, and the resulting integer is the price.

For example, is Sasha names 1213121, Vova can remove the substring 1312, and the result is 121.

It is allowed for result to contain leading zeros. If Vova removes all digits, the price is considered to be 0.

Sasha wants to come up with some constraints so that Vova can’t just remove all digits, but he needs some arguments supporting the constraints. To start with, he wants to compute the sum of all possible resulting prices after Vova’s move.

Help Sasha to compute this sum. Since the answer can be very large, print it modulo 109+7.

Input
The first and only line contains a single integer n (1≤n<10105).

Output
In the only line print the required sum modulo 109+7.

Examples
inputCopy
107
outputCopy
42
inputCopy
100500100500
outputCopy
428101984
Note
Consider the first example.

Vova can choose to remove 1, 0, 7, 10, 07, or 107. The results are 07, 17, 10, 7, 1, 0. Their sum is 42.

讨价还价
每次测试的时限1秒
每个测试的内存限制256 MB
输入标准输入
输出标准输出
有时要讨价还价并不容易。目前,Sasha和Vova无法达成协议:Sasha提出了尽可能高的价格,然后Vova希望从价格中删除尽可能多的数字。更详细地讲,Sasha命名了一个整数价格n,Vova从价格中删除了(连续)数字的非空子字符串,其余数字弥补了缺口,最后得到的整数就是价格。

例如,Sasha的名称为1213121,Vova可以删除子字符串1312,结果为121。

允许结果包含前导零。如果Vova删除所有数字,则价格视为0。

Sasha希望提出一些约束,以便Vova不能只删除所有数字,但他需要一些支持约束的参数。首先,他想计算Vova搬家后所有可能产生的价格之和。

帮助Sasha计算此和。由于答案可能非常大,因此请以109 + 7模数打印。

输入
第一行也是唯一一行包含一个整数n(1≤n<10105)。

输出
在唯一的行中,打印所需的总和模109 + 7。

例子
inputCopy
107
outputCopy
42
inputCopy
100500100500
outputCopy
428101984
笔记
考虑第一个例子。

Vova可以选择删除1、0、7、10、07或107。结果为07、17、10、7、1、0。它们的总和为42。

solution

/*
题意:
+ 给出一个数字,选出连续一段去掉,剩下部分合起来。
+ 求出所有选择方案剩下数字的和。
思路:
+ 枚举每个数位的贡献。对于当前位x,如果删后面的,分删1-i位i种情况,贡献为x*10^i。如果删除前面的,数值直接为x,所以贡献为前面的删除方案数*x。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 1e9+7;
LL pows[maxn], f[maxn];
int main(){
	string s;  cin>>s;  s="0"+s;
	LL n=s.size()-1;
	pows[0] = 1;
	for(LL i = 1; i <= n; i++)
		pows[i] = pows[i-1]*10%mod;
	for(LL i = 1; i <= n; i++)//前面的删除方案数
		f[i] = f[i-1]+i*pows[i-1]%mod;
	LL ans = 0;
	for(LL i = 1; i <= n; i++){
		LL num = s[i]-'0';
		ans = (ans+num*f[n-i]%mod)%mod;
		if(i != 1){
			ans += i*(i-1)/2%mod*pows[n-i]%mod*num%mod;
			ans %= mod;
		}
	}
	cout<<ans<<"\n";
	return 0;
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页