T1

Problem

洛谷

Solution

感觉我写的也不是正解。。。

我是先找出每个循环节的长度l。。。然后用快速幂求出10 ^ k % l的值。。

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
ll read()
{
ll x = 0; int zf = 1; char ch;
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * zf;
} ll T[1000005]; int main()
{
ll n = read(), m = read(), k = read(), x = read();
T[0] = x;
int l;
for (int i = 1; i <= n; i++)
{
x = (x + m) % n;
T[i] = x;
if (T[i] == T[0])
{
l = i;
break;
}
}
ll ans = 1, now = 10;
while (k > 0)
{
if (k % 2 == 1) ans = ans * now % l;
now = now * now % l;
k /= 2;
}
printf("%d\n", T[ans]);
}

T2

Problem

洛谷

Solution

首先可以证明当Ai在A中大小排名和Bi在B中大小排名相等时,其差的平方时最小的。。

因此只要控制排名相等就可以了。。所以先排序,然后建立一个排名在A中和B中位置的关系。。。最后用归并排序求逆序对。。

证明(两个数):设A中两个数a < b,B中两个数c < d

ans1 = (a - c) ^ 2 + (b - d) ^ 2 = a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 - 2 * a * c - 2 * b * d

ans2 = (a - d) ^ 2 + (b - c) ^ 2 = a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 - 2 * a * d - 2 * b * c

又因为a * c + b * d - a * d - b * c = (a - b) * (c - d) > 0

所以ans1 < ans2

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
ll read()
{
ll x = 0; int zf = 1; char ch;
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * zf;
}
ll ans = 0;
int X[100005], T[100005];
struct node
{
int val, id;
}a[100005], b[100005]; int cmp(node x, node y)
{
return x.val < y.val;
} void merge(int xl, int xr, int yl, int yr)
{
int l = xl, r = yr, now = l;
while (xl <= xr && yl <= yr)
{
if (X[xl] <= X[yl]) T[now++] = X[xl++];
else
{
T[now++] = X[yl++];
ans = (ans + xr - xl + 1) % 99999997;
}
}
while (xl <= xr) T[now++] = X[xl++];
while (yl <= yr) T[now++] = X[yl++];
for (int i = l; i <= r; i++) X[i] = T[i];
} void mergesort(int l, int r)
{
if (l == r) return;
int mid = (l + r) >> 1;
mergesort(l, mid);
mergesort(mid + 1, r);
merge(l, mid, mid + 1, r);
} int main()
{
int n = read();
for (int i = 1; i <= n; i++)
a[i].val = read(), a[i].id = i;
for (int i = 1; i <= n; i++)
b[i].val = read(), b[i].id = i;
sort(a + 1, a + n + 1, cmp);
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++)
X[b[i].id] = a[i].id;
mergesort(1, n);
printf("%lld\n", ans);
}

[NOIP2013D1]的更多相关文章

随机推荐

  1. 题解 Luogu P3623 [APIO2008]免费道路

    [APIO2008]免费道路 题目描述 新亚(New Asia)王国有 N 个村庄,由 M 条道路连接.其中一些道路是鹅卵石路,而其它道路是水泥路.保持道路免费运行需要一大笔费用,并且看上去 王国不可 ...

  2. xpath解析数据

    xpath解析数据 """ xpath 也是一种用于解析xml文档数据的方式 xml path w3c xpath搜索用法 在 XPath 中,有七种类型的节点:元素.属 ...

  3. Linux 设置系统时间和时区1.Centos

  4. BalkanOI 2018 Parentrises(贪心+基础DP)

    题意 https://loj.ac/problem/2713 思路 对于 \(\text{P1}\) 的档,首先可以看出 \(O(n^3)\) 的方法,即用 \(O(n^3)\) 的 \(\text{ ...

  5. CentOS yum换源

    1.备份系统自带yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.进入yum源配 ...

  6. 深入理解Java虚拟机(第二版)中《长期存活对象将进入老年代》的实践

  7. js循环中使用async/await踩过的坑

    最近写koa的时候遇见需要在循环中使用async/await的情况,当然第一反应就是直接上forEach,然后就直接翻车了... 直接上代码: function handleSql(val) { re ...

  8. [python]目录及文件操作

    Python OS模块和shutil模块 获取路径 # 获取当前路径 pwd = os.getcwd() # 获取上级路径 a_pwd = os.path.abspath(os.path.dirnam ...

  9. MySQL 列出存储过程

    MySQL  列出存储过程 接下来介绍如何列出MySQL数据库中的所有存储过程,并显示存储过程源代码的一些非常有用的语句. MySQL为提供了一些有用的语句,可以更有效地管理存储过程.这些语句包括列出 ...

  10. introduce explaining variable 引入解释变量

    一段复杂的计算的表达式(一般 逻辑判断  if(a!=1 && b!=Null && a>b  ) && .... 直接在代码中参与到 代码的逻辑 ...