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 ...
随机推荐
- js获取url參数值的两种方式具体解释
有个url例如以下: http://passport.csdn.net/account/login? from=http%3a%2f%2fwrite.blog.csdn.net%2fpostedit ...
- 转:【AI每日播报】从TensorFlow到Theano:横向对比七大深度学习框架
http://geek.csdn.net/news/detail/139235 说到近期的深度学习框架,TensorFlow火的不得了,虽说有专家在朋友圈大声呼吁,不能让TensorFlow形成垄断地 ...
- Win7如何修改开机动画
1 使用魔方美化大师可以替换WIN7的开机画面,我们用该软件打开一个开机动画,居然是一张BMP的图片. 2 在PS中可以发现,这张有21000像素长,大小12MB,可见所谓的动画其实是一个一个动作 ...
- Spine U3D整合流程问题
Spine U3D整合流程问题 What: 公司2d项目开发,动画外包的spine.本来在spine里面一切正常,但是导入u3d运行库的时候动画切换的时候原来的动画是好的,一旦切换了就乱帧了. 如下结 ...
- Java线程总结(转)
作者的blog:(http://blog.matrix.org.cn/page/Kaizen) 首先要理解线程首先须要了解一些主要的东西,我们如今所使用的大多数操作系统都属于多任务,分时操作系统.正是 ...
- 【Python3 爬虫】03_urllib.error异常处理
urllib.error可以接受来自urllib.request产生的异常.urllib.error有两个方法:①URLError ②HTTPError URLError URLError产生的原因 ...
- AngularJs学习笔记(3)——scope
AngularJS启动并生成视图时,会将根ng-app元素同$rootScope进行绑定. $rootScope是所有$scope对象的最上层,是AngularJS中最接近全局作用域的对象 . 一个n ...
- Audio简介
本片只简单从硬件角度简介Audio AC97/HDA Audio总线分两种: (1)I2S (2)HDA HD Audio spec Audio verb table是用来初始化audio的,一个au ...
- should + mocha 搭建简单的单元测试环境
快速搭建测试环境,详细用法请百度和访问两者的github mocha: http://mochajs.org/ should: https://github.com/shouldjs/should.j ...
- in App Purchases一个注意事项
在completeTransaction中通过transaction.originalTransaction.payment.productIdentifier得到的productIdentifier ...