hdu 2837 坑题。

Calculation


Time Limit: 2000/1000 MS
(Java/Others)    Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 1414    Accepted
Submission(s): 291

Problem Description

Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10)
for all n bigger than zero. Please calculate f(n)%m. (2 ≤ n , m ≤ 10^9, x^y
means the y th power of x).

Input

The first line contains a single positive integer T.
which is the number of test cases. T lines follows.Each case consists of one
line containing two positive integers n and m.

Output

One integer indicating the value of f(n)%m.

Sample Input

2 24 20 25 20

Sample Output

16 5

Source

2009
Multi-University Training Contest 3 - Host by WHU


 1 /**
2 a ^ b % c= a ^ ( b % phi ( c ) + phi ( c ) ) % c 条件:(b>=c) phi(n)为n的欧拉函数值
3 我想说(⊙o⊙)…,完全搞不懂标程
4 **/
5 #include<iostream>
6 #include<stdio.h>
7 #include<cstring>
8 #include<cstdlib>
9 using namespace std;
10 typedef __int64 LL;
11
12 LL Euler(LL n)
13 {
14 LL temp =n,i;
15 for(i=2;i*i<=n;i++)
16 {
17 if(n%i==0)
18 {
19 while(n%i==0)
20 n=n/i;
21 temp=temp/i*(i-1);
22 }
23 }
24 if(n!=1) temp=temp/n*(n-1);
25 return temp;
26 }
27 LL pow_mod(LL a,LL b,LL p){
28 LL ans=1;
29 while(b)
30 {
31 if(b&1) ans=(ans*a)%p;
32 b=b>>1;
33 a=(a*a)%p;
34 }
35 return ans;
36 }
37 LL fuck(LL a,LL n,LL m)
38 {
39 LL i,ans=1;
40 for(i=1;i<=n;i++)
41 {
42 ans=ans*a;
43 if(ans>=m) return ans;
44 }
45 return ans;
46 }
47 LL dfs(LL n,LL m){
48 LL ans,p,nima;
49 if(n==0) return 1;
50 if(n<10) return n;
51 p = Euler(m);
52 ans=dfs(n/10,p);
53
54 nima = fuck(n%10,ans,m);
55 if(nima>=m){
56 ans=pow_mod(n%10,ans%p+p,m);
57 if(ans==0) return m;
58 }
59 else{
60 ans=pow_mod(n%10,ans,m);
61 }
62 return ans;
63 }
64 int main()
65 {
66 int T;
67 LL n,m;
68 scanf("%d",&T);
69 while(T--){
70 scanf("%I64d%I64d",&n,&m);
71 printf("%I64d\n",dfs(n,m)%m);
72 }
73 return 0;
74 }

hdu 2837 坑题。

时间: 2024-08-29 08:44:35

hdu 2837 坑题。的相关文章

hdu 5455 Fang Fang 坑题

Fang Fang Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5455 Description Fang Fang says she wants to be remembered.I promise her. We define the sequence F of strings.F0 = ‘‘f",F1 = ‘‘ff",F2 = ‘‘cff",F

HDU 1498 50 years, 50 colors(最小点覆盖,坑题)

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1635    Accepted Submission(s): 892 Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating

hdu 4920 Matrix multiplication(矩阵坑题)

http://acm.hdu.edu.cn/showproblem.php?pid=4920 被这道题虐了一下午,啥也不说了.继续矩阵吧. 超时就超在每步取余上,要放在最后取余,再者注意三个循环的次序. #include <stdio.h> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <cma

hdu 4738 无向图缩点断桥 // 细节坑题

Caocao's Bridges 题意:给个无向图,求出边权最小的桥. 一看,直接缩点,若无桥,输出-1,有桥,遍历下边,更新最小..分分钟搞定,以为IA的..一交wa... 坑点:1:若原图不连通,则无须派人去!输出0!: 2:若桥的权是0,则还有派一个人把炸弹拿去,输出1! 3:有重边.(按多条边算). 哎!记住这个教训!以后做题 1:考虑边界或者特殊数据!(边权为0!n==1等) 2:考虑原图连通性!(这次考虑了原图就强连通..没有考虑根本不连通!) 3:重边.这题的重边是按重边算(不是一

随手练——数独 HDU - 5547 坑!坑!坑!

题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 解题思想:随手练-- 数独 POJ - 2676 (回溯法+DFS) HDU 的这题实在是太坑了,M 数组开成 int 就过不了,改成 char 就过了.对着别人AC的代码,一点点试,到最后才试出来,数组的问题,但是不能理解啊,什么鬼,这也错?? 然后发现题目描述里有一句:Each test case starts with an empty line followed by

hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画

【BZOJ-1952】Area [坑题] 仙人掌DP + 最大点权独立集(改)

1952: [Sdoi2010]Area Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 73  Solved: 23[Submit][Status][Discuss] Description 小猪iPig来到了一个叫做pigsty的城市里,pigsty是一座专门为小猪所准备的城市,城市里面一共有n个小区给小猪们居住,并且存在许多条无向边连接着许多小区.因为这里是一个和谐的城市,所以小猪iPig准备在这个城市里面度过他的余生.若干年之后小猪iPig

湘潭大学OJ1200ProblemCRC(摸拟,坑题)

题目描述 现实中的网络通讯不够理想,经常会有bit从0变1,从1变0...为了检验是否出错 需要用到循环冗余校验CRC CRC冗余检验码包含两个部分 k位信息位+n位校验位,可由以下的步骤得到: 1.将要传送的数据分段,每段k个bit,如果不足k位用0补齐 2.对于每个长度为k的01序列M, 先在M后面加n个0得到新的M'. 3.选定一个(n+1)位的01序列做为除数P,对M'做**模2的除法,得到一个n位的余数R(即校验位). 4.将R接到M的后面就得到了M的 CRC冗余检验码 请写一个程序模

求解概率的坑题

D - Problem D Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Description We choose an integer K (K > 0). Then we generate N (N > 0) integers one by one randomly, each of them is in range [0, K - 1], and