Codeforces Round #324 (Div. 2) (快速判断素数模板)
蛋疼的比赛,当天忘了做了,做的模拟,太久没怎么做题了,然后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) (快速判断素数模板)的更多相关文章
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)
D. Dima and Lisa Dima loves representing an odd num ...
- 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 ...
- 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 ...
- Codeforces Round #324 (Div. 2) (哥德巴赫猜想)
题目:http://codeforces.com/problemset/problem/584/D 思路: 关于偶数的哥德巴赫猜想:任一大于2的偶数都可写成两个素数之和. 关于奇数的哥德巴赫猜想:任一 ...
- Codeforces Round #324 (Div. 2) A B C D E
A,水题不多说. #include<bits/stdc++.h> using namespace std; //#define LOCAL int main() { #ifdef LOCA ...
- Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想
原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...
- Codeforces Round #324 (Div. 2) C (二分)
题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...
- 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 ...
随机推荐
- [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket错误解决方法总结
今天做一个特殊的业务处理,用JDBC连接SQLServer数据库载入驱动的时候,报例如以下错误: java.sql.SQLException: [Microsoft][SQLServer 2000 D ...
- 设计模式学习笔记--备忘录(Mamento)模式
写在模式学习之前 什么是设计模式:在我们进行程序设计时,逐渐形成了一些典型问题和问题的解决方式,这就是软件模式:每个模式描写叙述了一个在我们程序设计中常常发生的问题,以及该问题的解决方式:当我们碰到模 ...
- 对象内部属性[[Class]]
1.概述 所有的typeof返回值为‘object’的对象都包含一个内部属性[[Class]],我们将它可以看做内部的分类,而非传统面向对象意义的分类.这个属性无法直接访问,一般通过Object.pr ...
- 一个好用的短连接服务,mark备用
http://to.ly/api.php? longurl=http://www.example.com 当中http://www.example.com 是你所须要转换的长链接地址.经过一个简单的g ...
- 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬
1 触摸 Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能. 2 单点触摸 打开触摸开关和触摸方式 setTouchEnabled(true); setTouchMode(kCCT ...
- Spark on Yarn 集群运行要点
实验版本:spark-1.6.0-bin-hadoop2.6 本次实验主要是想在已有的Hadoop集群上使用Spark,无需过多配置 1.下载&解压到一台使用spark的机器上即可 2.修改配 ...
- Docker的Jenkins Pipeline工作流
原文地址:http://www.youruncloud.com/blog/127.html 分享主题 一个软件产品的开发周期中,尤其是敏捷开发,持续集成和持续部署是必不可少的环节,而随着产品的丰富,模 ...
- RabbitMQ 学习笔记(一)特点
RabbitMQ 的具体特点 可靠性: RabbitMQ 使用一些机制来保证可靠性, 如持久化.传输确认及发布确认等. 令灵活的路由: 在消息进入队列之前,通过交换器来路由消息.对于典型的路由功能,R ...
- FPGA和DSP间基于SRIO的高速通信系统设计
作者:陈婷,岳强,汪洋 解放军信息工程大学 摘要: 现代信号处理系统通常需要在不同处理器之间实现高速数据通信,SRIO协议由于高效率.低延时的特性被广泛使用.本文研究了在FPGA和DSP两种处理器之间 ...
- C/C++ 错误笔记-在给结构体中的指针赋值时,要注意该指针是否已指向内存空间
先来看下面的例子: #include <stdlib.h> #include <string.h> #include <stdio.h> #pragma warni ...