题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2165&cid=1431

快速幂。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int tt,n,cn[];
int qq[],we;
struct node
{
int w,id;
}q[];
#define mod 997
int er(int lf,int rf,int key)
{
int l=lf,mid;
int r=rf;
while(l<=r)
{
mid = l+(r-l)/;
if(q[mid].w==key)
return mid;
else if(q[mid].w>key)
{
r=mid-;
}
else
{
l=mid+;
}
}
return -;
}
int cmp(const void *a,const void *b)
{
struct node *aa=(struct node *)a;
struct node *bb=(struct node *)b;
return aa->w-bb->w;
}
int mult_mod(int a,int b)//计算 (a*b)%c.
{
a%=mod;// 利用二分思想减少相乘的时间
b%=mod;
ll ret=;
while(b)
{
if(b&)
{
ret+=a;
ret%=mod;
}
a<<=;
if(a>=mod) a%=mod;
b>>=;
}
return ret;
}
int pow_mod(int x,int n)//x^n%n
{
if(n==) return x%mod;
x%=mod;
int tmp=x;
int ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp);
tmp=mult_mod(tmp,tmp);
n>>=;
}
return ret;
}
void init()
{
memset(cn,,sizeof(cn));
tt=;
for(int i=;i<=;i++)
{
q[tt].id=i;
q[tt++].w=pow_mod(i,n);
}
for(int i=;i<=;i++)
{
q[tt].id=i;
q[tt++].w=pow_mod(i,n);
}
for(int i=;i<=;i++)
{
q[tt].id=i;
q[tt++].w=pow_mod(i,n);
}
qsort(q,tt,sizeof(q[]),cmp);
cn[]=;
for(int i=;i<tt;i++)
{
if(q[i].w==q[i-].w)
{
cn[i]=;
cn[i-]=;
}
else cn[i]=;
}
}
char s[];
int main()
{
int T,da;
bool FF;
scanf("%d",&T);
for(int z=;z<=T;z++)
{
FF=true; scanf("%d",&n);
init();
we=;
scanf("%s",s);
int l=strlen(s);
for(int i=;i<l;i=i+)
{
da=s[i]-'';
for(int j=i+;j<=i+;j++)
{
da=da*+(s[j]-'');
}
int t=er(,tt-,da);
if(t==-||cn[t]>)
{
printf("No Solution\n"); FF=false;
break;
}
else
{
qq[we++]=q[t].id;
}
}
if(FF)
{
for(int i=;i<we;i++)
printf("%c",qq[i]);
printf("\n"); }
// printf("\n");
}
return ;
}

SDUT2165:Crack Mathmen(快速幂)的更多相关文章

  1. sdut2165 Crack Mathmen (山东省第二届ACM省赛)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/24270265 本文出自:http://blog.c ...

  2. sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

  3. Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)

    Crack Mathmen TimeLimit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Since mathmen take security ...

  4. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  5. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  6. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

  7. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  8. GDUFE-OJ 1203x的y次方的最后三位数 快速幂

    嘿嘿今天学了快速幂也~~ Problem Description: 求x的y次方的最后三位数 . Input: 一个两位数x和一个两位数y. Output: 输出x的y次方的后三位数. Sample ...

  9. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

随机推荐

  1. iOS Json转换模型库:YYModel

    iOS Json转换模型库:YYModel   其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle.MJExtension)了,推荐他只是因为他高端的性能和容 ...

  2. 【C语言】09-字符串

    一.字符串简介 * 在Java中,一个字符串可以用String类型来存储 String s = "MJ"; C语言中没有String这种类型.其实字符串就是字符序列,由多个字符组成 ...

  3. 关于iOS手势

    引: 前几天遇到一个坑,又仔细分析了一下事件的原理,不得不承认苹果的文档还是写的挺好的,网上就搜不到有几篇博客是介绍这个的,都是一些关于基本的用法的.这里纪录一下. 1.关于事件响应链. a.硬件接收 ...

  4. ubuntu 14.04安装

    最近想搞spark.没有集群环境,想先在单机上跑.但是两年前一直用到现在的virtualbox已经太老,很多源都停更了.所以准备装ubuntu14.04. 0. 更新源.163的源有问题,用的是soh ...

  5. Mongodb 创建索引

    db.getCollection('ct_project').ensureIndex({'pro_code':1})  创建索引 db.getCollection('ct_project').ensu ...

  6. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  7. PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件(转)

    首先确定不是开启socks openssl phpinfo就可以知道 下载phpmailer   地址:https://github.com/PHPMailer/PHPMailer 下载完整, 个人和 ...

  8. mysql语句优化认识

  9. C++类型转化分析(1)

    仔细想想地位卑贱的类型转换功能(cast),其在程序设计中的地位就象goto语句一样令人鄙视.但是它还不是无法令人忍受,因为当在某些紧要的关头,类型转换还是必需的,这时它是一个必需品. 不过C风格的类 ...

  10. HTML: 文檔流是什麼?

    [作者好人]:http://www.nowamagic.net/librarys/veda/detail/1190 文档流 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流. ...