湖南省第十二届大学生计算机程序设计竞赛$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的更多相关文章

  1. 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...

  2. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  3. js sort() reverse()

    数组中存在的两个方法:sort()和reverse() 直接用sort(),如下: ,,,,,,,,,,,]; console.log(array.sort());ps:[0, 1, 2, 2, 29 ...

  4. [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 ...

  5. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  8. [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 ...

  9. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

随机推荐

  1. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

  2. 查看TOMCAT内存使用情况 以及修改方法

    查看TOMCAT内存使用情况 <% double total = (Runtime.getRuntime().totalMemory()) / (1024.0 * 1024); double m ...

  3. 由IEnumerable和IEnumerator的延伸

    相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是 ...

  4. Citrix 服务器虚拟化之六 Xenserver虚拟机创建与快照

    Citrix 服务器虚拟化之六  Xenserver虚拟机创建与快照 在Xenserver上可以创建Windows和Linux等虚拟机,Xenserver支持大部分的主流操作系统,可以使用 XenCe ...

  5. OpenCV 4 Python高级配置—安装setuptools,matplotlib,six,dateutil,pyparsing 完整过程

    Matplotib 是python 的一个绘图库,里头有各种各样的绘图方法,可以用Matplotib 显示图像,放大图像,保存图像等等,对于OpenCV处理图像具有非常大的帮助.但是,安装Matplo ...

  6. Vim 7.4.1952 with Python/Ruby/Lua/Perl/C Syntax built for Ubuntu 16.04 x86_64

    The default Vim provided by Ubuntu 16.04 even did not have Python support. That's insane. I say, wha ...

  7. WebService的简单实现

    WebService的简单实现 一.socket主机创建和使用过程 1.socket()//创建套接字 2.Setsockopt()//将套接字属性设置为允许和特定地点绑定 3.Bind()//将套接 ...

  8. java 子类、父类中静态代码块、字段,非静态代码块、字段以及构造函数的初始化顺序和次数

    一个类中的数据初始化顺序是面试官非常喜欢出的面试题之一,本文用一个实例来介绍java中子类.父类中静态代码块.字段,非静态代码块.字段以及构造函数的执行顺序和次数. 一.包结构

  9. 【JS学习笔记】关于function函数

    函数的基本格式 function 函数名() { 代码: } 函数的定义和调用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  10. iframe自适应高度问题

    我页面中的iframe <iframe name="mainFrame" id="mainFrame" src="/account/${page ...