zoukankan      html  css  js  c++  java
  • HDU 4704 SUM(费马小定理)

    题意:求2的n-1次方

    解题思路:费马小定理 

    解题代码:

     1 // File Name: 4704.c
     2 // Author: darkdream
     3 // Created Time: 2013年09月08日 星期日 20时32分42秒
     4 
     5 #include<stdio.h>
     6 #include<string.h>
     7 #include<stdlib.h>
     8 #include<time.h>
     9 #include<math.h>
    10 #define LL long long
    11 #define maxn 100009
    12 #define mod 1000000007
    13 //freopen("/home/plac/problem/input.txt","r",stdin);
    14 //freopen("/home/plac/problem/output.txt","w",stdout);
    15 
    16 
    17 char str[maxn];
    18 LL a[maxn];
    19 LL POW (LL n ,LL k )
    20 {
    21    LL ans = 1;
    22    LL p = n;
    23    while(k)
    24    {
    25        if(k &1)
    26            ans =(ans * p)%mod ;
    27        p = p*p %mod;
    28        k >>=1;
    29    }
    30    return ans;
    31 }
    32 LL len ;
    33 LL chu(LL k){
    34    LL temp = 0;
    35    for(int i = len;i >=1 ;i --)
    36    {
    37       temp = temp*10 + a[i];
    38       if(temp > k )
    39         temp = temp%k;
    40    }
    41  return temp;
    42 }
    43 void jian()
    44 {
    45   for(int i = 1;i <= len ;i ++)
    46   {
    47      if(a[i] >= 1)
    48      {
    49       a[i] -= 1;
    50        return;
    51      }
    52      else 
    53          a[i] = 9;
    54   }
    55 }
    56 int main(){
    57     while(scanf("%s",&str[1]) != EOF)
    58     {
    59        len = strlen(&str[1]);
    60       for(int i = 1; i<= len ;i ++)
    61           a[i] = str[len-i+1] - '0';
    62        jian();    
    63        LL k = chu(mod-1);
    64        printf("%lld
    ",POW(2,k));
    65     }
    66 
    67 return 0 ;
    68 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    冷门JS技巧
    JavaScript小技巧整理篇(非常全)
    JS实现标签页切换效果
    MySQL主从配置详解
    mysql主从复制(超简单)
    1.4isAlive()方法
    1.3currentThread()方法
    1.2.4注意Sysyem.out.println与i--
    1.2.3实例变量与线程安全
    1.2.2实现Runnable接口
  • 原文地址:https://www.cnblogs.com/zyue/p/3308788.html
Copyright © 2011-2022 走看看