题目: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. Use auto keyword in C++11

    Now compile your program with g++ -std=c++ your_file.cpp -o main

  2. Qt中图像的显示与基本操作

    Qt可显示基本的图像类型,利用QImage.QPxmap类可以实现图像的显示,并且利用类中的方法可以实现图像的基本操作(缩放.旋转). 1. Qt可显示的图像类型 参考Qt的帮助文档,可支持的类型,即 ...

  3. phpunit安装参考

    我主要参考看PHPunit参考手册https://phpunit.de/manual/current/zh_cn/installation.html 然后按照测试成功否检验,参考了http://blo ...

  4. 打包bat等文件成exe,双击运行不显示dos窗口,exe不报毒

    准备材料如下bat和vbs直接新建文本,然后改后缀就可以建出来了(后面发现exe运行vbs来启动bat不报毒)下面内容就是要把这些文件打包成exe,双击exe后打开图片test.jpg,不显示dos窗 ...

  5. [转]Web.config配置文件详解(新手必看)

    本文转自:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.config配 ...

  6. CSS3 3D Transform

    CSS3 3D Transform 原文:http://www.w3cplus.com/css3/css3-3d-transform.html 三维变换使用基于二维变换的相同属性,如果您熟悉二维变换, ...

  7. node系列:琐碎备忘

    cmd 全局与本地路径 查看:默认 查看本地路径:npm config get cache,默认和nodejs安装目录同一目录 查看全局路径:npm config get prefix,默认c盘app ...

  8. ASP.NET MVC系列 框架搭建(二)之仓储层的优化

    大神勿喷,小神默默学. 会了就是不值一提的东西,不会就是绝对的高大上. 最后上传源码.希望能给读者带来一些新的认识及知识. 还没上过头条..各位大神,请点支持一下小弟. 陆续更新.更新到你会为止!! ...

  9. linux系统

    ● ubuntu提示"sudo: unable to resolve host volcano: No such file or directory" 修改/etc/hosts,在 ...

  10. NetBeans 8.0 连接远程服务器

    step: ① 新建项目 -- PHP -- 来自远程服务器的 PHP 应用程序 -- 下一步 ② 名称和位置 项目名称:自己取: 源文件夹:通过 “浏览” 选择本地项目文件夹.注意文件夹内不能有 n ...