Lightoj 1054 - Efficient Pseudo Code

题目连接:

  http://www.lightoj.com/volume_showproblem.php?problem=1054

题目大意:

  给出n,m,问n^m的所有因子之和是多少?

解题思路:

  补充知识:

  1:对于一个数字n=p1^t1+p2^t2+p3^t3+.........+pn^tn。求n因子和等价于n所有因子的子集所对应值相加之和—(p1^0+p1^1+p1^2+......+p1^t1)*(p2^0+p2^1+......+p2^t2)*.....*(pn^0+pn^1+......+pn^tn).

  2:等比数列求和公式:

  3:除法取余:(a/b)%p == a%(b%p)/b%p;

        a/b%p == a*b^(p-2)%p;(当p是素数)   证明:有费马小定理可知:p是素数,(b,p) = 1, b^(p-1)%p == 1,a/b%p == a/b*1%p == a/b*b^(p-1)% == a*b^(p-2)%p.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int mod = 1000000007;
 5 const int maxn = 7000;
 6 typedef long long LL;
 7 int isprime[maxn], prime[maxn*10];
 8 LL sum, k;
 9
10 void Isprime ()
11 {//筛选出需要的素数
12     int i, j;
13     for (i=2, k=0; i<70000; i++)
14         if (!prime[i])
15         {
16             isprime[k ++] = i;
17             for (j=i; j<70000; j+=i)
18                 prime[j] = 1;
19         }
20        // printf ("%d\n", k);
21 }
22
23 LL Pow (LL x, LL y)
24 {//快速幂求x^y
25     LL num = 1;
26     while (y)
27     {
28         if (y % 2)
29             num = (num * x) % mod;
30         x = (x * x) % mod;
31         y /= 2;
32     }
33     return num;
34 }
35 LL solve (LL x, LL y)
36 {
37     LL num;
38     num = (Pow(x, y) - 1);
39     num = (num * Pow(x-1, mod-2)) % mod;
40     return (num + mod) % mod;
41 }
42
43 int main ()
44 {
45     LL t, n, m, l = 0;
46     Isprime ();
47     scanf ("%lld", &t);
48     while (t --)
49     {
50         scanf ("%lld %lld", &n, &m);
51         LL a, b, i;
52         i = 0;
53         sum = 1;
54         while (i < k)
55         {
56             if (1 == n)
57                 break;
58             a = b = 0;//统计还有的素数因子和因子的个数
59             if (n % isprime[i] == 0)
60             {
61                 a = isprime[i];
62                 while (n % isprime[i] == 0)
63                     {
64                         b ++;
65                         n /= isprime[i];
66                     }
67                 sum = (sum * solve(a, b*m+1) ) % mod;
68             }
69             i ++;
70
71         }
72         if (n != 1)
73             sum = (sum * solve(n, m+1)) % mod;
74             printf ("Case %lld: %lld\n", ++l, sum);
75     }
76     return 0;
77 }
时间: 2024-10-01 05:25:38

Lightoj 1054 - Efficient Pseudo Code的相关文章

Light OJ 1054 Efficient Pseudo Code 求n^m的约数和

题目来源:Light OJ 1054 Efficient Pseudo Code 题意:求n的m次这个数的所有的约数和 思路:首先对于一个数n = p1^a1*p2^a2*p3^a3*-*pk^ak  约束和s = (p1^0+p1^1+p1^2+-p1^a1)(p2^0+p2^1+p2^2+-p2^a2)-(pk^0+pk^1+pk^2+-pk^ak) 然后就是先求素数表 分解因子 然后求p1^0+p1^1+p1^2+-p1^a1 这是一个以1开始的等比数列 就是(1-q^n)/(1-q) 最

1054 - Efficient Pseudo Code

   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Sometimes it's quite useful to write pseudo codes for problems. Actually you can write the necessary steps to solve a particular problem. In this problem you are given a ps

Android Programming: Pushing the Limits -- Chapter 2: Efficient Java Code for Android

Android's Dalvik Java 与 Java SE 进行比较 Java代码优化 内存管理与分配 Android的多线程操作 Android’s Dalvik Java 与 Java SE 进行比较: @.Dalvik虚拟机是register-based machine:Java SE虚拟机是stack machine. @.从Android 2.2 版本开始引进JIT(Just In Time)编译器,之前是纯解释器. @.Android SDK 使用dx这个工具把Java SE s

Oracle Applications Multiple Organizations Access Control for Custom Code

文档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code Checked for relevance on 12-JAN-2011 See Change Record This document discusses how to update the customization code that is affected by the access co

PatentTips - Method and Apparatus to Support Virtualization with Code Patches

BACKGROUND As recognized in Revision 2.0 of the Intel? Virtualization Technology Specification for the Intel? Itanium? Architecture (VT-I), dated April 2005 (hereinafter "the VT-I Specification"), conventional operating system (OS) designs typic

计算机算法常用术语中英对照(分为两部分 其中一部分表格形式 )

第一部分 Data Structures 基本数据结构 Dictionaries 字典 Priority Queues 堆 Graph Data Structures 图 Set Data Structures 集合 Kd-Trees 线段树 Numerical Problems 数值问题 Solving Linear Equations 线性方程组 Bandwidth Reduction 带宽压缩 Matrix Multiplication 矩阵乘法 Determinants and Perm

【转】编程词汇

很实用的编程英语词库,共收录一千五百余条词汇. 第一部分: application 应用程式 应用.应用程序 application framework 应用程式框架.应用框架 应用程序框架 architecture 架构.系统架构 体系结构 argument 引数(传给函式的值).叁见 parameter 叁数.实质叁数.实叁.自变量 array 阵列 数组 arrow operator arrow(箭头)运算子 箭头操作符 assembly 装配件 assembly language 组合语

Method, apparatus, and system for speculative abort control mechanisms

An apparatus and method is described herein for providing robust speculative code section abort control mechanisms. Hardware is able to track speculative code region abort events, conditions, and/or scenarios, such as an explicit abort instruction, a

【转】Java 专业词汇

原址:http://blog.csdn.net/xiaojunjuns1/article/details/52729861 abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存取 ['.kses]'(n.入口,使用权) algorithm                     n.算法 ['.lg.riem] annotation                     [Java