Problem C. GSS and Bubble Sort

19人阅读 评论(0) 收藏 举报
分类:

Problem C. GSS and Bubble Sort
Input file: standard input
Output file: standard output
Time limit: 1 seconds
Memory limit: 512 mebibytes
Do you remember the problem “Time Limit Exceeded” last year? Here is the code GSS wrote in that problem.
include < stdio.h>
int main() {
int n;
int *vec;
scanf(”%d”, &n);
vec = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
int t;
scanf(”%d”, &t);
vec[i] = t;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 1; j++)
if (vec[j] > vec[j+1])
swap(vec[j], vec[j+1]);
/* some other code takes O(1) time */
return 0;
}
As you are GSS’s team mate, your task is to calculate the expected time the code will run with an input of size n,
(0 < n < 109
). The time is measured by how many times the function swap is called. You should note that the
input is a permutation of {1, 2, … , n} in the original problem.
Input
Input contains multiple (about 1000) test cases, please process to the end of input.
Each test cases contains an integer n as described above.
Output
For each test case, output one line with the answer.
If your answer is p/q, then you should output p × q
109+5 module 1000000007 (109 + 7).
Examples
standard input standard output
1
2
0
500000004
Note
In the second sample, there are two possible input {1, 2} and {2, 1}, so the expected time the function swap is
called is (0 + 1)/2 = 1/2, and 1 × 2
1e9+5 module 1e9 + 7 is 500000004.
题意:求1~n的全排列的逆序数的和为分子,全排列的个数为分母,然后对1e9+7取模。
题解:找规律,化简后为(n*(n-1)/4)%(1e9+7)。然后用逆元求解即可。

//标程
#include <bits/stdc++.h>
using namespace std;

const int mod = 1000000007;
long long pow_mod(long long a, long long b)
{
    long long ret = 1;
    while (b)
    {
        if (b & 1) {
            ret = ret * a % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

int main()
{
    long long n;
    while (cin >> n)
    {
        long long ret = n * (n - 1) % mod;
        ret = ret * pow_mod(4, mod - 2) % mod;
        cout << ret << "\n";
    }
    return 0;
}
查看评论

今日头条杯 2018 年首届湖北省大学生程序设计竞赛 (网络赛)Problem B.(计算几何)

Problem B. GSS and Interesting SculptureInput file: standard inputOutput file: standard outputTime l...
  • qq_41593380
  • qq_41593380
  • 2018-04-15 20:11:45
  • 234

网络数据请求展示为无限轮播图 ViewPager+XListView+Fragment+ImageLoader

一.  MainActivity.java package androidthree_1509d.MyViewpager; import android.support.v4.app.Fragm...
  • IT666DHW
  • IT666DHW
  • 2017-09-13 22:18:56
  • 233

jarvisoj WEB +MISC writeup

WEB PORT 51 Login LOCALHOST 神盾局的秘密 IN a mess Easy Gallery Simple Injection 方法一 方法二 api的调用 PHPINFO...
  • Ni9htMar3
  • Ni9htMar3
  • 2016-12-26 22:16:57
  • 4850

OSGI学习笔记一(事件传递)

一、定义在jujianzh传递事件的类
  • waterbbx
  • waterbbx
  • 2014-11-07 18:35:08
  • 9713

微信小程序版翻牌游戏

一、新建一个quick start项目看看结构 在微信开发工具点击添加项目,选择 无appid,勾上"在当前目录中创建quick start 项目"。 可以看到一共有两个目录 pa...
  • u010950854
  • u010950854
  • 2017-03-08 21:41:51
  • 7827

iOS--常见的几种数据存储方式

1、iOS应用数据存储的常用方式 XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2、...
  • baojie1022
  • baojie1022
  • 2015-05-07 09:53:38
  • 12774

生产环境部署Hadoop+Spark+HBase+Hue

总结一下在生产环境部署Hadoop+Spark+HBase+Hue等产品遇到的问题、提高效率的方法和相关的配置。 集群规划 假设现在生产环境的信息如下: 服务器数量:6操作系统:Centos7...
  • u010022051
  • u010022051
  • 2016-03-22 11:36:31
  • 2583

Unable to load configuration. - include file...+java/net/PlainSocketImpl.java:177:-1

2016-1-4 22:24:46 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomca...
  • yhl_jxy
  • yhl_jxy
  • 2016-01-04 22:35:25
  • 1287

数据库的增删改查加遍历

一、jar包的导入,和相关类的创建:二、创建数据库,在其中创建表。三、功能选择程序,主要引用method类中的方法:package com.cn.database; import java.sql....
  • chaiwuxuan
  • chaiwuxuan
  • 2016-08-01 16:20:39
  • 1985

js+html 实现关系拓扑图

最近要做一个功能。需要用到关系拓扑图。做了一些调研。发现相关插件很多接下来做个分析 1.yWorks 德国人做的。支持jAjax,html ,php。 官网 https://www.yworks.co...
  • wx5701619
  • wx5701619
  • 2017-08-03 15:04:01
  • 16150
    动漫

    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 4万+
    积分: 2289
    排名: 2万+
    博客专栏
    最新评论