%1e9+7问题收录

题目一:
问题描述:求一个序列(数组)所有非空子序列乘积之和。结果对 1000000007 取模。
 
#include <iostream>
#include <cstdio>
using namespace std;
const int M = 1e9 + 7;
#define ll long long
int main()
{
    int n;
    ll x, ans = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &x);
        ans += ans * x % M + x;
        ans %= M;
    }
    printf("%lld", ans);
    return 0;
}

看题目当成一道规划题了,没多想。

解题核心ans += ans * x % M + x

即每个数乘N次加它自身。

 
 
题目二:
链接:https://ac.nowcoder.com/acm/contest/11189/B
问题描述:求一个大数,每一位与每一位按位差值绝对值之和。如100-99=|1+0+0-9-9|=17
#include <iostream>
using namespace std;
const int mod = 1e9 + 7;
long long num[100005] = {0};
string R;
int main()
{
    int i, n, ans = 0;
    num[0] = 1, num[1] = 17;
    for (i = 2; i <= 1e5; i++)
        num[i] = (num[i - 1] * 10 + 9) % mod;
    cin >> R;
    n = R.length();
    for (i = 0; i < n; i++)
        ans = (ans + (R[i] - '0') * num[n - i - 1]) % mod;
    cout << (ans - 1 + mod) % mod;
    return 0;
}

  解题时考虑到了,除了进位时加的数字与众不同,其余全是1,然后推出了进位时的数字服从等差数列,但因为忽略了如110-109=8这种也算进位的情况所以跑偏了。然后我们再来欣赏一下python代码吧:

n = int(input())
a = n-1
n //= 10
c = 7
p = int(1E9+7)
while n > 0:
    a = a+c*n
    if c == 7:
        c = 9
    n //= 10
print(a % p)

  

 
猜测:大数阶乘,大数的排列组合等,一般都要求将输出结果对1e9+7取余.
(参考 https://blog.csdn.net/mlm5678/article/details/87910446  )
这类问题应该都是解题核心不会复杂,反复乘积。
原文地址:https://www.cnblogs.com/thx2199/p/14589059.html