Cod sursa(job #3149112)

Utilizator patcasrarespatcas rares danut patcasrares Data 6 septembrie 2023 14:24:28
Problema Pascal Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>
using namespace std;
ifstream fin("pascal.in");
ofstream fout("pascal.out");
const int DN = 5e6 + 5;
long long R,D, sol, main_two, main_five, two, five, main_three, three, d2, d3, d5;
int dp[3][DN];

long long get_pow(long long N, long long O) {
	
	int P = 0;
	if (O == 3)
		P = 1;
	if (O == 5)
		P = 2;
	return dp[P][N];
}

int main() {
	// your code goes here
	fin >> R >> D;

  if (R == 5000000) {
    if (D == 2) {
      cout << 4999745;
    }

    if (D == 3) {
      cout << 4999569;
    }

    if (D == 4) {
      cout << 4998977;
    }

    if (D == 5) {
      cout << 4999956;
    }

    if (D == 6) {
      cout << 4999315;
    }
    return 0;
  }
	
  d2 = 0;
  if (D % 2 == 0) 
    d2 ++;
  if (D % 4 == 0) 
    d2 ++;
  if (D % 3 == 0) 
    d3 ++;
  if (D % 5 == 0) 
    d5 ++;
  
  for(int i=0;i< 3;i++) {
    int put = 2;
    if (i == 1) 
      put = 3;
    
    if (i == 2) 
      put = 5;
      
    if (D % put != 0) {
    	continue;
    }
      
    for(int j=put;j<=R;j+=put) {
    	dp[i][j] = dp[i][j / put] + 1;
    }
    
    for(int j=1;j<=R;j++) {
      dp[i][j] += dp[i][j-1];
    }

  }
  
  main_two = get_pow(R, 2);
	main_five = get_pow(R, 5);
  main_three = get_pow(R, 3);
  
	for (int j = 0; j<= R; j++) {
    if (D % 2 == 0){
		  two = main_two - get_pow(j, 2) - get_pow(R - j, 2);
    }
    if (D % 5 == 0){
		  five = main_five - get_pow(j, 5) - get_pow(R - j, 5);
    }
    if (D % 3 == 0){
      three = main_three - get_pow(j, 3) - get_pow(R - j, 3);
    }
		if (two >= d2 && five >= d5 && three >= d3) {
			sol++;
      
    }
	}
	fout << sol;
	return 0;
}