CSU 1810 Reverse
湖南省第十二届大学生计算机程序设计竞赛$H$题
规律,递推。
这种问题一看就有规律。可以按位统计对答案的贡献。即第$1$位对答案作出了多少贡献,第$2$位对答案作出了多少贡献.....累加和就是答案。
先写一个暴力的程序来找找规律:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} const int maxn=;
struct X
{
int p;
}s[maxn];
int n;
int f[maxn][maxn]; int main()
{
while(~scanf("%d",&n))
{
memset(f,,sizeof f);
for(int i=;i<=n;i++) s[i].p=i; for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
for(int k=i;k<=(i+j)/;k++) swap(s[k],s[j-(k-i)]); for(int k=;k<=n;k++) f[s[k].p][k]++; for(int k=i;k<=(i+j)/;k++) swap(s[k],s[j-(k-i)]);
}
} for(int i=;i<=n;i++)
{
int sum=;
for(int j=;j<=n;j++)
{
// sum=sum+(int)pow(10.0,j-1)*f[i][j];
printf("%3d ",f[i][j]);
}
printf("\n");
// printf("%d ",sum);
}
printf("\n"); }
return ;
}
上面的代码中,$f[i][j]$表示$i$这一位,所有交换中,在$j$位出现了几次;答案就是$\sum\limits_{i = 1}^n {\left( {s[i]×\left( {\sum\limits_{j = 1}^n {f[i][j]×{{10}^{j - 1}}} } \right)} \right)} $。
输出来看一下$n=10$和$n=11$时候的情况,看看$f[i][j]$有没有什么规律:

通过观察可以发现:
$[1].$每一行的和都是一样的,$n=x$时,每一行的和$cnt[x]$都是一样的,并且$cnt[x]=x+cnt[x-1]$。
$[2].$第$i$行的贡献${\sum\limits_{j = 1}^n {f[i][j]×{{10}^{j - 1}}} }$可以由第$i-1$行的贡献${\sum\limits_{j = 1}^n {f[i-1][j]×{{10}^{j - 1}}} }$递推而来。
也就是说,我们可以$O(n)$效率得到每一位的贡献,然后乘上输入的那个权值就是答案了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi = acos(-1.0), eps = 1e-;
void File()
{
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ; while (!isdigit(c)) c = getchar();
while (isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const LL mod = 1e9 + ;
const int maxn = ;
LL a[maxn], cnt[maxn], POW[maxn], sPOW[maxn], num[maxn];
char s[maxn];
int n; int main()
{
cnt[] = ;
for (int i = ; i <= ; i++) cnt[i] = (cnt[i - ] + i) % mod;
POW[] = ; sPOW[] = ;
for (int i = ; i <= ; i++)
{
POW[i] = (LL) * POW[i - ] % mod;
sPOW[i] = (sPOW[i - ] + POW[i]) % mod;
} while (~scanf("%d%s", &n, s))
{
memset(num, , sizeof num);
memset(a, , sizeof a); num[] = (cnt[n] - (n - ) + mod) % mod;
a[] = (num[]*POW[] % mod + (sPOW[n - ] - sPOW[] + mod) % mod) % mod; int L = , R = n - ;
for (int i = ; i < n / ; i++)
{
L++, R--; num[i] = (num[i - ] - (R - L + ) + mod) % mod;
a[i] = (a[i - ] + sPOW[R] - sPOW[L - ] + mod) % mod;
a[i] = (a[i] + ((num[i] - i + mod) % mod)*POW[i] % mod) % mod;
a[i] = (a[i] - (((num[i - ] - i + mod) % mod)*POW[i - ] % mod) + mod) % mod;
} num[n - ] = num[];
a[n - ] = (num[n - ] * POW[n - ] % mod + sPOW[n - ]) % mod; L = , R = n - ; LL d = ;
for (int i = n - ; i >= (n ) / ; i--)
{
L++, R--; num[i]= (num[i + ] - (R - L + ) + mod) % mod;
a[i]= (a[i + ] + sPOW[R] - sPOW[L - ] + mod) % mod;
a[i] = (a[i] + ((num[i] - d + mod) % mod)*POW[i] % mod) % mod;
a[i] = (a[i] - (((num[i + ] - d + mod) % mod)*POW[i + ] % mod) + mod) % mod;
d++;
} LL ans = ;
for (int i = ; s[i]; i++) ans = (ans + (LL)(s[i] - '')*a[n-i-] % mod) % mod;
cout << ans << endl;
}
return ;
}
CSU 1810 Reverse的更多相关文章
- 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- js sort() reverse()
数组中存在的两个方法:sort()和reverse() 直接用sort(),如下: ,,,,,,,,,,,]; console.log(array.sort());ps:[0, 1, 2, 2, 29 ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- codeforces #275 div2题解
A题大意: 给你l,r,问你在l~r之间,是否存在 a和b互质 , b和c互质 ,但是 a,c不互质 的情况:其中l<=a<b<c<=r;如果存在,就输出a,b,c;不存在就输 ...
- Leetcode::Pathsum & Pathsum II
Pathsum Description: Given a binary tree and a sum, determine if the tree has a root-to-leaf path su ...
- 揭开redis神秘面纱
一直听别人说NoSQL,以前一直不明白,这到底是什么东西,今天听过我们涛哥的讲解,略有小感,特此小记. NoSQL(NoSQL = Not Only SQL),意为反SQL运动,是一项全新的数据库革命 ...
- Entity Framework实体模型 入门视频教程
Entity Framework实体模型 入门视频教程 恢复内容开始--- 第一步 创建一个 控制台应用程序 第二步 创建一个ADO.NET 数据实体模型 DbModel.edmx 需要跟数据库进行连 ...
- 定时器Timer不定时
订餐系统之定时器Timer不定时 经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统 ...
- Spring.Net-创建对象
1:通过构造函数创建对象 在配置文件中,指明对象类型<object id="" type="类型全名,程序集名" /> 在代码中指定对象 ...
- mybatis判断list为空
在传入的map或者对象里面存在一个list,此时想要判断list是否为空,可以 <if test="spids.size()>0 " > and SPid not ...
- 【转载】Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门
Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...
- C语言之函数的介绍
函数的介绍 遇到的问题: 1.代码看起来特别多,不简洁 2.修改起来非常麻烦,需要所有用到的地方都修改 函数就可以解决上述这两个问题 函数可以理解为一个打包带,就是把一段代码打包起来,用到的时候只要写 ...
- Server Tomcat v6.0 at localhost was unable to start within 45 seconds
在eclipse里启动tomcat的时候出现以下的错误: Server Tomcat v6.0 at localhost was unable to start within 45 seconds. ...