蛋疼的比赛,当天忘了做了,做的模拟,太久没怎么做题了,然后C题这么简单的思路却一直卡到死,期间看了下D然后随便猜了下,暴力了下就过了。

A.找一个能被t整除的n位数,那么除了<=10以外,其他都可以用长度为n的10或100,1000 。。。 来往上加几个数而得到

#include <iostream>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <string.h>
using namespace std; int getwei(int x)
{
int sum=;
while(x)
{
sum++;
x=x/;
}
return sum;
}
int main()
{
int n,t;
scanf("%d%d",&n,&t);
if(n==&&t==)
printf("-1");
else if(n==)
{
printf("%d",t);
}
else
{
int save;
for(int i=;i<;i++)
{
if(i%t==)
{
save=i;
break;
}
}
printf("%d",save);
for(int i=;i<n;i++)
{
printf("");
}
}
return ;
}

B.很好找的公式题,((27^n-7^n)%MOD+MOD)%MOD。n很不大所以直接暴力就行了。

#include <iostream>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <string.h>
using namespace std;
#define MOD 1000000007 int main()
{
int n;
scanf("%d",&n);
long long tmp=;
long long tmp1=;
for(int i=;i<n;i++)
{
tmp=tmp*;
tmp%=MOD;
tmp1=tmp1*;
tmp1%=MOD;
}
tmp-=tmp1;
tmp= (tmp%MOD+MOD)%MOD;
cout<<tmp<<endl;
return ;
}

C.思维题,给出了长度为n的两个字符串a和b,要你找到一个c串使得c串分别与a串和b串的相同位置不同字符数恰好为t。可以这样想,如果在某个位置i上

如果a[i]==b[i],那么可以贡献a和b相异值1或0

如果a[i]!=b[i],那么要么贡献串a的相异值为1,要么贡献b的相异值为1,要么同时贡献a和b的相异值1.

因为ab的相异值必须得相等,那么最小的相异值为:所有a[i]!=b[i]的个数和为sumaneb,(sumaneb+1)/2 。如果这个值>t那么必定无解,否则一定有解。

如果有解,因为a[i]==b[i]的情况是可控的,所以尽量先把这种情况全部加进来,如果这种情况全部算进来却还是小于t,那么就将a[i]!=b[i]的情况变成对a和b同时贡献的情况。

这样想其实挺复杂的,判断情况就要想,还得推公式一种情况弄个c串。

看了大神的代码立马觉得涨姿势,果真自己的思路太弱。大神的做法是先得出一个c串,使得和ab串完全不相等。然后从a[i]==b[i]的情况中一个一个加入相等的情况,如果全部设为相等后还是不够,那就冲a[i]!=b[i]中加入相等的,如果全部加完还是不行就输出-1.

用这种思路的大神开场不到20分钟就把这题A了。 。。

#include <iostream>

using namespace std;

int n, t;
string a;
string b;
string c; int main() {
cin >> n >> t;
cin >> a >> b;
int u = n;
c = a;
for (int i = ; i < n; i++) {
char x = 'a';
if (a[i] == x || b[i] == x) x = 'b';
if (a[i] == x || b[i] == x) x = 'c';
c[i] = x;
}
for (int i = ; i < n; i++) {
if (a[i] == b[i] && t != u) {
c[i] = a[i];
u--;
}
}
int z = ;
for (int i = ; i < n; i++) {
if (a[i] == b[i] || u == t) continue;
if (z == ) {c[i] = a[i]; z = ;}
else {c[i] = b[i]; u--; z = ;}
}
if (u == t) cout << c << "\n";
else cout << "-1\n";
}

D.给出一个3到10^9的整数n,然后要求输出三个素数a,b,c使得a+b+c=n.

哥德巴赫猜想:任何一个大于二的偶数都可以分解为两个素数和。

我的猜想是任意一个<=10^9的偶数,都可以快速分拆为两个素数和。所以就可以将n-3快速拆分为两个素数。

其实我这种猜想风险还太大,我使用了rabbin算法快速的判断一个数是不是素数,但是这种方法虽然快但不是绝对正确的,有一定的失误率。

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std; #define S 100
typedef unsigned long long LL; LL modular_multi(LL x,LL y,LL mo)
{
LL t;
x%=mo;
for(t=;y;x=(x<<)%mo,y>>=)
if (y&)
t=(t+x)%mo;
return t;
} LL modular_exp(LL num,LL t,LL mo)
{
LL ret=,temp=num%mo;
for(;t;t>>=,temp=modular_multi(temp,temp,mo))
if (t&)
ret=modular_multi(ret,temp,mo);
return ret;
} bool miller_rabbin(LL n)
{
if (n==)return true;
if (n<||!(n&))return false;
int t=;
LL a,x,y,u=n-;
while((u&)==) t++,u>>=;
for(int i=;i<S;i++)
{
a=rand()%(n-)+;
x=modular_exp(a,u,n);
for(int j=;j<t;j++)
{
y=modular_multi(x,x,n);
if (y==&&x!=&&x!=n-)
return false;
///其中用到定理,如果对模n存在1的非平凡平方根,则n是合数。
///如果一个数x满足方程x^2≡1 (mod n),但x不等于对模n来说1的两个‘平凡’平方根:1或-1,则x是对模n来说1的非平凡平方根
x=y;
}
if (x!=)///根据费马小定理,若n是素数,有a^(n-1)≡1(mod n).因此n不可能是素数
return false;
}
return true;
}
int main()
{
int n;
scanf("%d",&n);
if(n==) printf("1\n3");
else if(n==)
printf("2\n2 3");
else
{
n-=;
for(int i=;i<=n;i++)
{
if( miller_rabbin(i) ==true)
{
if(miller_rabbin(n-i) == true)
{
printf("3\n3 %d %d",i,n-i);
break;
}
}
}
}
return ;
}

我乱搞的算法

更确定的算法是,从n->1,先求出一个素数复杂度约等于10*sqrt(n),然后剩下来的就暴力几乎不要时间。

E.这题想了挺久的。

其实问题可以简化为

1   2   3   4  ...  n

a1 a2 a3 a4 ..   an

标号为ai的要到i位置。 然后每次交换为|i-j|费用,ij为标号。

我的思路是既然现在交换是看之间相隔的距离了,那么我把一个长的交换分拆成小的交换可以换的更好的效果。具体做法是:

对于没一个ai,如果ai>i,这ai这个数需要往右移,将这个位置标记为>

如果ai<i,这ai这个数需要往左移,将这个位置标记为<

如果ai=i,这ai这个数已经到达位置,将这个位置标记为0

比如一组数据

1 2 3 4 5

3 4 5 1 2

就可以变为

1  2 3  4  5

> > > < <

接着对于没一个相邻或者中间只隔0的><的情况进行变换,知道所有的状态都为0

比如

3 4 5 1 2 (>>><<)->

3 4 1 5 2  (>><><)->

3 1 4 5 2  (><>><)->

1 3 4 5 2  (0>>><)->

1 3 4 2 5  (0>><0)->

1 3 2 4 5  (0><00)->

1 2 3 4 5  (00000)结束。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
#define N 2020 int f[N],tf[N];
int mylink[N];
int g[N];
int x[N*N],y[N*N];
int cnt; int main()
{
int ans=;
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",tf+i);
}
for(int i=;i<=n;i++)
{
scanf("%d",f+i);
mylink[f[i]]=i;
}
for(int i=;i<=n;i++)
{
tf[i]=mylink[ tf[i] ];
}
for(int i=;i<=n;i++)
{
if(tf[i]==i) g[i]=;
else if(tf[i]>i) g[i]=;
else g[i]=-;
}
while()
{
int flag=;
int pre=-;
int id=-;
for(int i=;i<=n;i++)
{
if(g[i]==-)
{
if(pre==)
{
swap(tf[id],tf[i]);
if(tf[id]>id) g[id]=;
else if(tf[id]==id) g[id]=;
else g[id]=-; if(tf[i]>i) g[i]=;
else if(tf[i]==i) g[i]=;
else g[i]=-;
flag=;
x[cnt]=id;
y[cnt]=i;
ans+=i-id;
cnt++;
if(tf[i]==i) break;
else
{
pre=;
id=i;
}
}
else
pre=-;
}
else if(g[i]==)
{
pre=;
id=i;
}
}
if(flag==) break;
}
printf("%d\n",ans);
printf("%d\n",cnt);
for(int i=;i<cnt;i++)
printf("%d %d\n",x[i],y[i]);
return ;
}

Codeforces Round #324 (Div. 2) (快速判断素数模板)的更多相关文章

  1. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  2. Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)

                                                     D. Dima and Lisa Dima loves representing an odd num ...

  3. Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂

    B. Kolya and Tanya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pro ...

  4. Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想

    D. Dima and Lisa Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

  5. Codeforces Round #324 (Div. 2) (哥德巴赫猜想)

    题目:http://codeforces.com/problemset/problem/584/D 思路: 关于偶数的哥德巴赫猜想:任一大于2的偶数都可写成两个素数之和. 关于奇数的哥德巴赫猜想:任一 ...

  6. Codeforces Round #324 (Div. 2) A B C D E

    A,水题不多说. #include<bits/stdc++.h> using namespace std; //#define LOCAL int main() { #ifdef LOCA ...

  7. Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想

    原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...

  8. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  9. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心

    E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

随机推荐

  1. 恼人的The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved...错误,无奈用Struts的bean:write替代了JSTL的C:out

    一个应用中有两个页面使用了JSTL的c:out输出,就类似这么简单三句 <c:if test="${!empty error}">       <h2>&l ...

  2. 【原】Ubuntu下使用teamviewer

    想尝试直接使用Xmanager打开Ubuntu桌面版,结果一直存在问题,迂回一下,尝试使用teamviewer解决,结果OK.办法如下: 在Ubuntu中,下载teamviewer,通过Windows ...

  3. 深入理解C/C++ [Deep C (and C++)] (2)

    好.接着深入理解C/C++之旅.我在翻译第一篇的时候.自己是学到不不少东西,因此打算将这整个ppt翻译完成. 请看以下的代码片段: #include <stdio.h> void foo( ...

  4. iOS 计步器的几种实现方式

    代码地址如下:http://www.demodashi.com/demo/11658.html 这篇文章介绍两种可以获取计步数据的方法,一种是采用CMPedometer获取手机计步器数据,另一种是采用 ...

  5. Python解微分方程

    1.求解常微分方程的步骤: from sympy import * init_printing() #定义符号常量x 与 f(x) g(x).这里的f g还可以用其他字母替换,用于表示函数 x = S ...

  6. C# socket编程 使用fleck轻松实现对话 https://github.com/statianzo/Fleck

    class Program { static void Main(string[] args) { FleckLog.Level = LogLevel.Debug; var allSockets = ...

  7. 关于struts、spring 和 hibernate的说明

    struts 是 web 框架 (jsp/action/actionfrom) hibernate 是 orm框架,处于持久层. spring 是容器框架,用于配置bean,并维护bean之间关系的框 ...

  8. Atitit  atiMail atiDns新特性 v2  q39

    Atitit  atiMail atiDns新特性 v2  q39 V1  实现了基础的功能 V2  重构..使用自然语言的方式 c.According_to_the_domain_name(&quo ...

  9. 基于RocketIO的高速串行协议设计与实现

    随着对信息流量需求的不断增长, 传统并行接口技术成为进一步提高数据传输速率的瓶颈.过去主要用于光纤通信的串行通信技术—SERDES正在取代传统并行总线而成为高速接口技术的主流.SERDES 是串行器) ...

  10. MII、GMII、RMII、SGMII、XGMII

    MII即媒体独立接口,也叫介质无关接口.它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1). 数据接口包括分别用于发送器和接收器的两条独立信 ...