NEU OJ 1651 Great number

循环节是2000000016

字符串读入,用一下高精度对2000000016取个模,用一下快速幂就可以算出答案了。

#include <cstdio>
#include <iostream>
#include<cstring>
using namespace std;
const long long MOD = 1e9+7;
long long mod1(char *a1,int b)
{

    long long  a[5000] = {0};
    long long c[5000] = {0};

    long long  i, k, d;
    k = strlen(a1);
    for(i = 0; i < k; i++)  a[i] = a1[k - i - 1] - ‘0‘;
    d = 0;
    for(i = k - 1; i >= 0 ; i--)
    {
        d = d * 10 + a[i];
        c[i] = d / b;
        d = d % b;
    }
    while(c[k - 1] == 0 && k > 1)  k--;
    return d;
}
struct matrix
{
    long long m[2][2];
}ans, base;

matrix multi(matrix a, matrix b)
{
    matrix tmp;
    for(int i = 0; i < 2; ++i)
    {
        for(int j = 0; j < 2; ++j)
        {
            tmp.m[i][j] = 0;
            for(int k = 0; k < 2; ++k)
                tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]% MOD) % MOD;
        }
    }
    return tmp;
}
int fast_mod(int n)  // 求矩阵 base 的  n 次幂
{
    base.m[0][0] = base.m[0][1] = base.m[1][0] = 1;
    base.m[1][1] = 0;
    ans.m[0][0] = ans.m[1][1] = 1;  // ans 初始化为单位矩阵
    ans.m[0][1] = ans.m[1][0] = 0;
    while(n)
    {
        if(n & 1)
        {
            ans = multi(ans, base);
        }
        base = multi(base, base);
        n >>= 1;
    }
    return ans.m[0][1];
}
char SS[1000];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",SS);
        //printf("%d\n",mod1(SS,2000000016));
        printf("%lld\n", fast_mod(mod1(SS,2000000016))%MOD);
    }
    return 0;
}
时间: 2024-10-28 14:22:49

NEU OJ 1651 Great number的相关文章

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

山东省赛题 NEU OJ 1444 线段树双标记

http://acm.neu.edu.cn/hustoj/problem.php?id=1444 OJ问题论坛发帖http://t.cn/zjBp4jd FAQ http://t.cn/zjHKbmN Linux问题看http://t.cn/aWnP1n 1444: Devour Magic 时间限制: 1 Sec  内存限制: 256 MB 提交: 129  解决: 21 [提交][状态][讨论版] 题目描述 In Warcraft III, Destroyer is a large flyi

Light OJ 1234 Harmonic Number 调和级数部分和

题目来源:Light OJ 1234  Harmonic Number 题意: 思路:没思路啊 这个是高数的东西 发散 n足够大时它无穷大 直接公式解 #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <iostream> using namespace std; const int

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

NEU OJ 1649 GMZ’s Pretty Number

先来一次线性素数筛,把1到10000000的素数都筛选出来,然后暴力跑一遍所有可能的值,打个表,查询的时候o(1)效率出解. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<cstring> using namespace std; const long N = 10000000; long

FZU OJ 2111 Min Number (贪心)

Problem 2111 Min Number Accept: 586    Submit: 1139 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choo

LeetCode OJ:Happy Number(欢乐数)

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

LeetCode OJ:Ugly Number(丑数)

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

XTU OJ 1210 Happy Number (暴力+打表)

Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number contain all digit 7 or only 1 digit other than 7. For example, 777 is a happy number because 777 contail all digit 7, 7177 and 87777 both happy number