题目: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. OpenCV Open Camera 打开摄像头

    这是一个用OpenCV2.4.10打开摄像头的一个例子,参见代码如下: #include <iostream> #include <stdio.h> #include < ...

  2. 获取当前 Windows 的安装序列号

    Dim s s = InputBox("当前Windows系统序列号为:", "Windows序列号", GetWindowsSN) WScript.Quit ...

  3. tomcat从下载到使用

    话说,某天正在和周公聊天下大事.被急促的电话铃声召唤回来,所谓江湖救急,于是远程一看.竟然是需要使用tomcat(汤姆家的猫),于是... 下面关于下载和配置tomcat的过程. ①.使用tomcat ...

  4. GTX 680 Kepler

    http://www.nvidia.com/object/nvidia-kepler.html http://www.geforce.com/hardware/desktop-gpus/geforce ...

  5. PHP 错误与异常 笔记与总结(10)错误处理器测试

    关联文件:myErrorHandler.php (上一篇) 先测试通知级别的错误的自定义处理: testErrorHandler.php <?php require_once 'myErrorH ...

  6. spring mvc传入参数不仅仅转换为json,还可以直接将json字符串转换为具体的java对象

    1.controller层 /** * 查看主播资料 * * @return */ @RequestMapping(value = { "/actor_details" }, me ...

  7. Google Chrome can not be run as root

    Ubuntu运行Chrome出现"Google Chrome can not be run as root"的解决方法 编辑启动文件:/opt/google/chrome/goog ...

  8. ThinkPHP验证码刷新随机数

    貌似因为IE的内核不支持重复,,所以要加个随机数..在代码中,,发现火狐的也不行..加了随机数后就可以了 <label class="img"><img id=& ...

  9. JDBC 增删查改

    public class MemberDaoImpl implements MemberDao { private Connection conn = null; public MemberDaoIm ...

  10. 数据库留言板例题:session和cookie区别

    session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...