题目: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数据解析

      关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSer ...

  2. nginx源码编译安装

    安装编译所需的包: [root@xaiofan ~]# yum install -y gcc gcc-c++ autoconf automake 安装nginx使用某些功能需要的包: [root@xa ...

  3. opengl中拾取操作的实现

    opengl采用一种比较复杂的方式来实现拾取操作,即选择模式.选择模式是一种绘制模式,它的基本思想是在一次拾取操作时,系统会根据拾取操作的参数(如鼠标位置)生成一个特定视景体,然后又系统重新绘制场景中 ...

  4. android之dialog

    先编写activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...

  5. 配置SMarty解析

    在 common/main.php中配置 View 组件 'view' => [ 'renderers' => [ 'tpl' => [ 'class' => 'yii\sma ...

  6. centos时间同步方法

    centos时间同步方法 电脑软硬件应用网 45IT.COM 时间:2012-12-08 18:09 作者:李本清 新装的服务器可能设置了错误的,需要调整时区并调整时间.如下是使用NTP来从一个时间服 ...

  7. 异步调试神器Slog,“从此告别看日志,清日志文件了”

    微信调试.API调试和AJAX的调试的工具,能将日志通过WebSocket输出到Chrome浏览器的console中  — Edit 92 commits 4 branches 3 releases ...

  8. GUN485项目的总结

    1.DMA中配置要放在串口的配置后面. 2.DMA有3种中断方式:传输完成.传输一半.传输错误 3.如果要用DMA容易造成串口数据还没发完就把485的控制脚拉低导致数据没发完.解决办法是DMA发送完成 ...

  9. 从 github 执行 git clone 一个大的项目时提示 error: RPC failed

    目前克隆一个比较大的项目,出现RPC failed的错误 Cloning into 'bigfiles'... remote: Counting objects: 190, done. remote: ...

  10. Latex公式换行、对齐

    http://blog.sina.com.cn/s/blog_64827e4c0100vnqu.html 换行后等式对齐 \begin{equation}\begin{aligned}R(S_2)&a ...