[简单题] Project Euler 601 Divisibility streaks

版权声明:本文为博主原创文章,未经博主允许随意转载。 https://blog.csdn.net/u014609452/article/details/79115753

(k+1)|(n+k) 就是 (k+1)|(n1)
这个函数就是最大的 k 使得 1,2,,k 都整除n-1吧
随便容斥下咯

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

inline ll Gcd(ll a,ll b){
  return !b?a:Gcd(b,a%b);
}

inline ll Sum(ll n,ll x){
  n/=x;
  return n*(n+1)/2*x+n;
}
inline ll Cnt(ll n,ll x){
  return n/x;
}


inline int f(int n){
  for (int i=0;;i++)
    if ((n+i)%(i+1)!=0)
      return i;
}
inline int g(int n){
  n--;
  for (int i=1;;i++)
    if (n%i!=0)
      return i-1; 
}

int main(){
  ll Ans=0,ans2=0;
  ll lcm=1,nxt;
  for (int i=1;i<=31;i++){
    ll n=1LL<<(i<<1);
    //for (int j=2;j<n;j++)
    //  if (f(j)==i)
    //ans2++;
    nxt=lcm*(i+1)/Gcd(lcm,i+1);
    Ans+=Cnt(n-2,lcm)-Cnt(n-2,nxt);
    lcm=nxt;
  }
  printf("%lld\n",Ans);
  return 0;
}
阅读更多

Divisibility

05-05

Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions:nn17 + 5 + -21 + 15 = 16 n17 + 5 + -21 - 15 = -14 n17 + 5 - -21 + 15 = 58 n17 + 5 - -21 - 15 = 28 n17 - 5 + -21 + 15 = 6 n17 - 5 + -21 - 15 = -24 n17 - 5 - -21 + 15 = 48 n17 - 5 - -21 - 15 = 18 nnWe call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.nnYou are to write a program that will determine divisibility of sequence of integers.nnnInputnnThe first line of the input contains two integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a space. nnThe second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.nnnOutputnnWrite to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it's not.nnnThis problem contains multiple test cases!nnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.nnThe output format consists of N output blocks. There is a blank line between output blocks.nnnSample Inputnn2nn4 7n17 5 -21 15nn4 5n17 5 -21 15nnnSample OutputnnDivisible nnNot divisible

没有更多推荐了,返回首页